Explain how to Handle Exceptions in .NET 2.0.
The different methods of handling the exceptions are:
1.
try
{
// code
}
catch(Exceptiontype *etype_object)
{
// code
}
2.
try
{
// code
}
catch(Exceptiontype *etype_object)
{
throw new Custom_Exception();
}
Exceptions should never be handled by catching the general System.Exception errors, rather specific exceptions should be caught and handled.
They are handled using try catch and finally. Finally is used to cleanup code as it's always executed irrespective of whether an exception has occurred or not.
Exampletry
{
FileInfo file=new FileInfo(@"c:\abc.txt")
}
catch(FileNotFoundException e)
{
//handle exception
}
Finally
{
//cleanup code here
}
If file is found, then finally will get invoked else both catch and finally will occur.