This documentation is archived and is not being maintained.

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.


int top = 0, bottom = 0, result = 0;
    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