Tuesday, 14 February 2012

Exceptional handling

In C#, exceptional handling provides an environment that will handle the errors in the code by using three different blocks.

· Try

· Catch

· Finally

Try block:

In try block,we can place the code which will have errors.

Syntax:

try

{

Statements://code in which error occured

}

Catch block:

Catch block handles the exception if it occurs.catch block is optional.if there is an error in try block then

only control goes to catch block.

In this block, we can handle the exception which occurred in try block.

Syntax:

Catch(exception e)

{

Statements;//handling the exception

}

Finally block:

Finally block is mandatory in exceptional handling in C #.this block exicutes irrespective of error occurred in try block.

If thereis no error in try block then control goes to finally block ,else goes to catch block and then comes to finally block.

Syntax:

finally

{

Statements;//code that will run irrespective of error

}

No comments:

Post a Comment