Export (0) Print
Expand All
Expand Minimize

Code: Catching an Exception (Visual C#)

Visual Studio .NET 2003
 

This example uses the try-catch blocks to catch division by zero as an exception.

Example

int top = 0, bottom = 0, result = 0;
try
{
    result = top / bottom;
}
catch (System.DivideByZeroException ex)
{
    Console.WriteLine("{0} Exception caught.", ex);
}

Compiling the Code

Copy the code and paste it into the Main method of a console application.

See Also

Code: Throwing an Exception (Visual C#) | try-catch | try-catch-finally | DivideByZeroException Class | General Language Example Topics

Show:
© 2015 Microsoft