C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Handling Multiple Exceptions In C#


Posted Date: 02 Oct 2009    Resource Type: Articles    Category: .NET Framework
Author: Muhammad JavedMember Level: Gold    
Rating: 1 out of 5Points: 10



Handling Multiple Exceptions In C#


To handle different types of exceptions, you can have one or more catch blocks in the try - catch
statement. The following example shows how you can catch three different exceptions:
* DivideByZeroException — Thrown when there is an attempt to divide an integral or decimal
value by zero.
* FormatException — Thrown when the format of an argument does not meet the parameter
specifications of the invoked method.
* Exception — Represents errors that occur during application execution.
This example handles the three different exceptions and then prints out a custom error message:

static void Main(string[] args)
{
int num1, num2, result;
try
{
Console.Write(“Please enter the first number:”);
num1 = int.Parse(Console.ReadLine());
Console.Write(“Please enter the second number:”);
num2 = int.Parse(Console.ReadLine());
result = num1 / num2;
Console.WriteLine(“The result of {0}/{1} is {2}”,
num1, num2, result);
}
catch (DivideByZeroException ex)
{
Console.WriteLine(“Division by zero error.”);
catch (FormatException ex)
{
Console.WriteLine(“Input error.”);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}

Typing in a numeric value for num1 and an alphabetic character for num2 produces the
FormatException exception in this program, which is caught and displayed like this?
Please enter the first number:6
Please enter the second number:a Input error.
Entering 0 for the second number throws the DivideByZeroException
exception, which is caught and
displays a different error message:
Please enter the first number:7
Please enter the second number:0
Division by zero error.
So far, all the statements are located in the Main() function. What happens if you have a function called
PerformDivision() that divides the two numbers and returns the result, like this?

class Program
{
static void Main(string[] args)
{
int num1, num2;
try
{
Console.Write(“Please enter the first number:”);
num1 = int.Parse(Console.ReadLine());
Console.Write(“Please enter the second number:”);
num2 = int.Parse(Console.ReadLine());
Program myApp = new Program();
Console.WriteLine(“The result of {0}/{1} is {2}”,
num1, num2,
myApp.PerformDivision(num1, num2));
}
catch (DivideByZeroException ex)
{
Console.WriteLine(“Division by zero error.”);
}
catch (FormatException ex)

Console.WriteLine(“Input error.”);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
private int PerformDivision(int num1, int num2)
{
return num1 / num2;
}
}

If num2 is zero, an exception is raised within the PerformDivision() function. You can either catch
the exception in the PerformDivision() function or catch the exception in the calling function — Main()
in this case. When an exception is raised within the PerformDivision() function, the system searches
the function to see if there is any catch block for the exception. If none is found, the exception is passed
up the call stack and handled by the calling function. If there is no try - catch block in the calling function,
the exception continues to be passed up the call stack again until it is handled. If no more frames exist
in the call stack, the default exception handler handles the exception and your program has a runtime error.
Note:Value in "value" in double inverted comma represents a string



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Multiple Exceptions In C#  .  Handling Multiple Exceptions In C#  .  Handling Multiple Exceptions In C Sharp  .  Exceptions Handling Multiple In C#  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Exception Handling in C#
Previous Resource: Custom Error in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use