try...catch statement is for exception handling. Its syntax is
try
{
... // exception may get thrown within execution of this block
}
catch (ExceptionA ex)
{
... // handle exception of type ExceptionA
}
catch (ExceptionB ex)
{
... // handle exception of type ExceptionB
}
finally
{
... // cleanup code
}
Here is a demo.
using System;
class Test
{
static int Calc(int x) { return 10 / x; }
static void Main()
{
try
{
int y = Calc(0); Console.WriteLine(y);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("x cannot be zero");
}
Console.WriteLine("program completed");
}
}
The output:
x cannot be zero
program completed
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |