How to handle exception in catch block in c#
Printable View
How to handle exception in catch block in c#
To handle exception in c# we use
try
{
//code
}
catch(Exception ex)
{
// Message to show exception.
}
Totally agree for his answer. and the if you have condition/statement that need to execute alyways then can use finally block. also try can have mulitple catch block.
C# provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process. The general form try-catch-finally in C# is shown below
try
{
// Statement which can cause an exception.
}
catch(Type x)
{
// Statements for handling the exception
}
finally
{
//Any cleanup code
}
Exception Handling in C#
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100/x;
Console.WriteLine(“This line is not executed”);
}
catch(DivideByZeroException de)
{
Console.WriteLine("Exception occured");
}
Console.WriteLine("Result is {0}",div);
}
}
Hi,
Use this following syntax
try
{
}
catch(Exception ee)
{
}