c#

How to use try catch block in c#?

Try catch block is used to handle exception in c#. In try block we write the executable code. In catch block we write the code which may give exception.

Syntax:

try{

}

catch{

}

finally{

}

Explanation:

Try:

Put suspected code here. When an exception occurs, the control will move to the catch block

Catch:

Catch the exception 

Finally: This block of code will always get executed whether an  exception occurs or not as long as there are no exceptions  within this block itself.If an Exception is thrown here  some outer try block should handle it Any Clean up code goes here. Especially to release any system resources such as file handles and network  connections.