Throwing and Catching Exceptions

Throwing Exceptions - C# , VB.Net ,

Exceptions are objects that encapsulate an irregular circumstance, such as when an application is out of memory, a file that cannot be opened, an attempted illegal cast etc. These exceptions objects that describe an error are created and then thrown with the throw keyword. By using a throw statement inside a catch block, we can change the resulting exception.

You should throw exceptions only when an unexpected or invalid activity occurs that prevents a method from completing its normal function. You can throw any type of Throwable object using the keyword throw. It interrupts the method and anything after the throw statement would not be executed, unless the thrown exception is handled.


static void Main(string[] args)
	//If there is no parameter found..
	if (args.Length == 0)
		throw new ArgumentException("No parameter found");


Private Shared Sub Main(args As String())
	'If there is no parameter found..
	If args.Length = 0 Then
		Throw New ArgumentException("No parameter found")
	End If
End Sub

Catch and throw Exception

Exception - C# , VB.Net ,

You can also throw a caught exception again using the throw statement. It is good programming practice o add information to an exception that is re-thrown to provide more information when debugging.


    int zero = 0;
    int val = 100 / zero;
    val = val + 1;
catch (DivideByZeroException ex)
    throw new DivideByZeroException("Plz dont try to divide by zero !!", ex);


	Dim zero As Integer = 0
	Dim val As Integer = 100 \ zero
	val = val + 1
Catch ex As DivideByZeroException
	Throw New DivideByZeroException("Plz dont try to divide by zero !!", ex)
End Try

throw; and throw ex;

Catch and throw Exception - C# , VB.Net ,

Exceptions contain a property called StackTrace. It contains the name of the methods on the current call stack, along with the file name and line number where the exception was thrown for each method. In .Net. you can preserve the stack trace is through the use of the "throw" only statement.

  //Your code here
catch (Exception ex)

throw ex;

In .Net however, when you do "throw ex", ex is being re-thrown, but the original stack trace info gets overriden. When you throw an exception using "throw ex" they loose the stack trace. It is then considerably harder to debug an application and to achieve appropriate trace messages. When simply using "throw" no data is lost and the whole exception together with the stack trace can be easily retrieved.

Exception Handling

Exceptions are the occurrence of some condition that changes the normal flow of execution. In .NET languages , Structured Exceptions handling is a fundamental part of Common Language Runtime. More about.... Exception Handling

System level Exceptions Vs Application level Exceptions

Exceptions are provide a structured, uniform, and type-safe way of controlling both system level and application level abnormal conditions. It can be generated by system or can be generated programmatically. More about.... System level Exceptions VS ..

Difference between Exception and Error

An exception is an Object of a type deriving from the System.Exception class. SystemException is thrown by the CLR (Common Language Runtime) when errors occur that are nonfatal and recoverable by user programs. More about.... Exception and Error

NEXT.....String Vs StringBuilder