Information Technology Reference
In-Depth Information
Handling the Exception
The previous example showed that attempting to divide by zero causes an exception. You can
modify the program to handle that exception by placing the code inside a try block and sup-
plying a simple catch clause. When the exception is raised, it is caught and handled in the catch
block.
static void Main()
{
int x = 10;
try
{
int y = 0;
x /= y; // Raises an exception
}
catch
{
... // Code to handle the exception
Console.WriteLine("Handling all exceptions - Keep on Running");
}
}
This code produces the following message. Notice that, other than the output message,
there is no indication that an exception has occurred.
Handling all exceptions - Keep on Running
Search WWH ::




Custom Search