Exception handling Questions

Q1)How to use Exception Handling?
Ans)
What is Exception?
An Exception is an unexpected error or problem.
What is Exception Handling?
It is a method to handle the error and solution to recover from it which makes the program to work smoothly.

What is try Block?
The try block consists of the code that might generate an error.
This try block must be associated with one or more catch blocks or by finally block.
The try block may not have a catch block associated with it every time but in this case,it must have a finally block associated with it instead of catch block.

Example:

Format1



try

{

//Code that might generate error

}

catch(Exception error)

{

}



Format2



try

{

//Code that might generate error

}

catch(ExceptionA error)

{

}

catch(ExceptionB error)

{

}



Format3



try

{

//Code that might generate error

}

finally

{

}



What is catch Block?
catch block is used to recover from error generated in the try block.
In case of multiple catch blocks,only the first matching catch block is excecuted.
you need to arrange the multiple catch blocks from specific exception type to more generic type.
If none of the matching catch blocks are able to handle the exception, the default behaviour of web page is to terminate the processing of the web page.

Example:

Format1



try

{

//Code that might generate error

}

catch(Exception error)

{

//Code that handle errors occurred in try block

}



Format2:



try

{

//Code that might generate error

}

catch(DivideByZeroException error)

{

//Code that handle errors occurred in try block

//It is the most specific error we are trying to catch

}

catch(Exception error)

{

//Code that handle errors occurred in try block

//It is not specific error we are catching

}

catch

{

//Code that handle errors occurred in try block

//It is the least specific error in hierarchy

}



What is finally Block?
The finally block contains the code that executes,whether an exception occurs or not.
The finally block is used to write code to close files,database connections,etc.
Only one finally block is associated with each try block.
finally block must appear only after the catch block.
If there are any control statements such as goto,break or continue in either try or catch block,the transfer happens only after the code in the finally block is excecuted.
If the control statements are used in finally block,there occurs a compile time error.

Example:

Format1



try

{

//Code that might generate error

}

catch(Exception error)

{

//Code that handle errors occurred in try block

}

finally

{

//Code to dispose all allocated resources

}



Format2



try

{

//Code that might generate error

}

finally

{

//Code to dispose all allocated resources

}
-----------------------------------------------
Q2)"Viewstate information does not automatically transfer from page to page. It cannot be tracked across pages". Is the statement true ?
Ans)
Select from following answers:

Yes
No
Maybe


Yes, the above statement is true.

Viewstate information cannot be tracked across pages. You can use session or querystring to track anything from one page to another.

Note: Do not store large amount of data on the page using viewstate because it will slow down the page load process.
------------------------------------------------
Q3) What is the use of throw statement?
Ans)
A throw statement is used to generate exception explicitly.
This throw statement is generally used in recording error in event log or sending an Email notification about the error.
Avoid using throw statement as it degrades the speed.

Example:

try

{

//Code that might generate error

}

catch(Exception error)

{

//Code that handle errors occurred in try block



throw; //Re throw of exception to add exception details in event log or sending email

}



//Code that handle errors occurred in try block

}

finally

{

//Code to dispose all allocated resources

}



Format2



try

{

//Code that might generate error

}

finally

{

//Code to dispose all allocated resources

}
----------------------------------------------------------

4)How to manage unhandled Exception?
Ans)
You can manage unhandled exception by configuring the element in web.config file.
It has 2 attributes:

Mode Attribute:It specifies how the custom error page should be displayed.
It has 3 values:
on - Displays custom error pages at both the local and remote client.
off - Disables custom error pages at both the local and remote client.
RemoteOnly - Displays custom error pages only at the remote client as it displays default asp.net error page for local machine.This is default setting in web.config file.
defaultRedirect:It is an optional attribute to specify the custom error page to be displayedwhen an error occurs.
The custom error page is displayed based on http error statusCode using error element inside the customError element.
If none of the specific statusCode matches with it, it will redirect to defaultRedirect page.
-----------------------------------------------
5)Difference between catch(Exception error) and catch
Ans)
The code below explains the difference between catch(Exception error) and catch


try

{

//Code that might generate error

}



catch(Exception error)

{

//Catches all Cls-complaint exceptions

}



catch

{

//Catches all other exception including the Non-Cls complaint exceptions

}