Try - Catch - Finally

You can create (or throw) an exception by using the keyword Throw

Throw New System.Exception 


General Catch Statement

try 
{

}
catch (System.DivideByZeroException ex)
{
   System.Windows.Forms.MessageBox.Show(ex.Message - ex.InnerMessage);
}
finally
{
}


Dedicated Catch Statements

try 
{}
catch (System.DivideByZeroException ex)
{}
catch (System.ArithmeticException ex)
{}
catch
{}
finally
{}

Finally

Any code in the Finally block is guaranteed to be executed regardless of whether an exception is thrown
A finally block can be created with or without catch blocks



Creating your own exceptions

They must derive (directly or indirectly) from the System.ApplicationException


public class MyCustomException 
   : System.ApplicationException
{

   public void New(string message)
   {
      MyBase.New(message)
   }
}


InnerException Property

You may want your catch block to take some corrective action first and then rethrow the exception to the outer try block
You may want to refer to the exception history and InnerException property of an exception retrieves the original/previous exception.





© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext