-
Junior Member
How to handle exception in catch block in c#
How to handle exception in catch block in c#
-
Junior Member
Re: 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.
}
-
Expert Member
Re: How to handle exception in catch block in c#
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.
-
Junior Member
Re: How to handle exception in catch block in c#
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);
}
}
Last edited by prsoorya; 07-29-2008 at 01:18 AM.
-
Junior Member
Re: How to handle exception in catch block in c#
Hi,
Use this following syntax
try
{
}
catch(Exception ee)
{
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules