What is the use of the exception variable in catch block c sharp ??The program is working fine without that variable....
Loading
What is the use of the exception variable in catch block c sharp ??The program is working fine without that variable....
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sinu JosephPosted May 26, 2013, 3:53 AM
The exceptions are anomalies that occur during the execution of a program.
"Exception is a runtime error which arises because of abnormal conditions in a condition in a sequence."
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 exception if one exists. The finally can be used for doing any clean up process.
The general form of try-catch-finally in c# is shown below:
try
{
// Statements which can cause an exception
}
catch(Type x)
{
// Statements for handling the exception
}
finally
{
// Any Cleanup Code
}
Example of Exception Handling:
using System;
namespace ConsoleApplication1
{
class main
{
public static void Main()
{
int[] a = new int[5];
int i;
try
{
for (i = 0; i < 7; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
}
catch (Exception ex)
{
Console.WriteLine("Please Check The Error Limits...");
}
for (i = 0; i < 5; i++)
{
Console.WriteLine("Array Element Is " + a[i]);
}
}
}
}
VulpesPosted May 25, 2013, 4:11 PM
1. You want to access one of its properties (Message, StackTrace, InnerException etc); or
2. You want to re-throw the exception so that it be caught by an earlier try/catch block.