IV-1 A Simple Exception Example
using System.Text;
using
System.Windows.Forms;
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
Windows Application1
{
public
partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
{
Private
void Form_Load(object sender, EventArgs e)
{
try
{
int y = 0;
int z = 10;
int result = 0;
result = z / y;
}
Catch (DivideByZeroException
x)
{
MessageBox.Show(x.Message);
}
}
}
IV-2 The Consequence of an Uncaught Exception
Catching one of C#’s standard exceptions, as the preceding program does, has a side benefit: It prevents abnormal program termination. When an exception is thrown, it must be caught by some piece of code somewhere. In general, if your program does not catch an exception, it will be caught by the C# runtime system. The trouble is that the runtime system will report an error and terminate the program. For instance, in this example, the index out-of-bounds exception is not caught by the program:
As mentioned earlier, the type of the exception must match the type specified in a catch statement. If it doesn’t, the exception won’t be caught. For example, the following program tries to catch an array boundary error with a catch statement for a DivideByZeroException (another of C#’s built-in exceptions). When the array boundary is overrun, an IndexOutOfRangeException is generated, but it won’t be caught by the catch statement. This results in abnormal program termination.
V-Using Multiple Catch Statement
In C#, You can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions means each catch block is used to handle different type of exception.
Source Code:
private
void Form1_Load(object sender, EventArgs e)
{
int[ ] k = {10,3,5,60 };
int [ ] = {2,0,3,0,5 };
for(int I = 0; I < y.Length; i++)
{
try
{
MessageBox.Show(k[i]+”/”+y
[i ] +” +is ”+k [i] /y [i] )
}
catch (ArithmeticException exc)
{
MessageBox.Show(exc.Message);
}
catch (IndexOut Of Range Exception
{
MessageBox.Show(exc.Message);
}
}
VI-Using
Finally
It is a reserved keyword in C#. The finally block will execute when the try/catch block leaves the execution, no matter what condition cause it. It always executes whether the try block terminates normally or terminates due to an exception. The main purpose of finally block is to release the system resources.
Source Code:
private void
btnNumber_Click(object sender, EventArgs e)
{
try
{
int x = int.Parse(this.textBox1.Text);
if (x
>= 0)
MessageBox.Show("Positive
Number");
else
MessageBox.Show("Negative
Number");
return;
}
catch(Exception ex)
{
MessageBox.Show("Error
found.");
return;
}
finally
{
MessageBox.Show("End
of process");
}
}
VII-System.Exception
he SystemException is a predefined exception class in C#. It is used to handle system related exceptions. It works as base class for system exception namespace. It has various child classes like: ValidationException, ArgumentException, ArithmeticException, DataException, StackOverflowException etc.
VIII-Throwing
Exception
You can explicitly throw an exception using the C# throw or the Visual Basic Throw statement. You can also throw a caught exception again using the throw statement. It is good coding practice to add information to an exception that is re-thrown to provide more information when debugging.
Source Code:
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Windows Application1
{
public partial class Form1 : Form
{
Public
Form1( )
{
InitialzeComponent(
) ;
}
private void button1_Click(object sender, EventArgs e)
{
String
st Divide (10,0).ToString();
MessageBox.Show(st);
}
int Divide (int x, int y)
{
if(y==0)
throw new Exception ("Divider can't be 0");
return
x / Y;
}
}
Source Code:
class Test
{
public int Div(int x, int y)
{
try
{
return
x / y;
}
catch (Exception e)
{
throw (e);
}
}
private void btnTest_Click(object sender, EventArgs e)
{
Test obj = new Test ();
}
try
{
int y = obj.Div();
}
catch (Exception el ) {
MessageBox.Show(el. Message.ToString() );\
return;
}
0 Comments