In
this article, I would like to know you about multiple catch blocks for try
block.
Actually
the Exception class is the super class for all the exceptions, so this
Exception class catch block should be at end of all catch blocks, if not it
shows an error message at compile time only i.e., A previous catch clause already catches all exceptions of this or
of a super type ('Exception').
So
that to avoid compile time errors, we have to set the order of different exception
catch blocks.
The
below example is the sample program that shows the order of the catch block exceptions :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSHARP_MultipleCatchBlocks
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("Enter a value : ");
int? numerator = int.Parse(Console.ReadLine());
Console.Write("Enter b value : ");
int? denominator = int.Parse(Console.ReadLine());
int? result = numerator / denominator;
Console.WriteLine("Division Of
numerator/denominator : {0}", result);
Console.ReadKey();
}
//// If catch
block with parameter class Exception is written first, then the following error
message come at compile time
//// A previous
catch clause already catches all exceptions of this or of a super type
('Exception')
//// If given
value is more than the datatype size then ArithmeticException Occurs
catch (DivideByZeroException ex) // Here DivideByZeroException is written first because it is
derived from ArithmeticException, if not it shows the compile time error as
same as above
{
Console.WriteLine(ex.Message.ToString() + "
DivideByZero Exception");
Console.ReadKey();
}
//// If given
value is more than the datatype size then ArithmeticException Occurs
catch (ArithmeticException ex)
{
Console.WriteLine(ex.Message.ToString() + "
Arithmetic Exception");
Console.ReadKey();
}
//// If given
value datatype is not supports by existing declared datatype then
FormatException Occurs
catch (FormatException ex)
{
Console.Write(ex.Message.ToString() + " Format
Exception");
Console.ReadKey();
}
// This catch
block should be last, because this is the super class for all exceptions
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString() + "
Base Exception");
Console.ReadKey();
}
}
}
}
Output:
When we
run the application it displays as :
Enter only
the integer values in numerator and denominator variables. It display as below
:
Now
enter the denominator value zero, then it executes the DivideByZeroException
catch block and
throws exception as shown below :
Now
enter the numerator or denominator value more than the size of the int datatype, then it executes the
ArithmeticException catch block and throws
exception as shown below :
Now enter the numerator or denominator value other than the int datatype, like string, float etc, then it executes the FormatException catch block and throws exception as shown below :
No comments:
Post a Comment
Note: only a member of this blog may post a comment.