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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted May 25, 2013, 12:11 PM EDT
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.
Sinu Joseph117 reputationPosted May 25, 2013, 11:53 PM EDT
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]);
}
}
}
}