•
An
exception is termed as an abnormal condition encountered by an application
during execution.
•
Exception
handling is the process of providing an alternative path of execution when the
application is unable to execute as desired.
•
Whenever
an error occurs, runtime creates an exception object and sends it to the
program in which the execution occurred. This action is known as throwing an
exception.
•
All
the exceptions are derived from the System.Exception class
Types
of Errors:
Syntax
errors: A syntax
error occurs when compiler cannot compile code. This occurs when statements are
not constructed properly, keywords are misspelled, or punctuation is omitted.
Run-Time
Errors: A
runtime error occurs when an application attempts to perform an operation,
which is not allowed at runtime such as divide by zero.
Logical
Errors: A
logical error occurs when an application compiles and runs properly but does
not produce the expected results.
|
Exception
Classes
|
Description
|
|
System.IO.IOException
|
Handles
I/O errors.
|
|
System.IndexOutOfRangeException
|
Handles
errors generated when a method refers to an array element, which is out of
its bound.
|
|
System.NullReferenceException
|
Handles
errors generated during the process of dereferencing a null object.
|
|
System.DivideByZeroException
|
Handles
errors generated during the process of dividing the dividend with zero.
|
|
System.InvalidCastException
|
Handles
errors generated during typecasting.
|
|
System.OutOfMemoryException
|
Handles
memory allocation to the application errors.
|
Try
Catch Finally
The try Block: Guards' statements that may throw an
exception. It defines the scope of the exception-handlers associated with it.
The catch Block: It takes an object of the exception
class as a parameter, which refers to the raised exception. When the exception
is caught, the statements within the catch block are executed.
The finally Block: It is used to execute a given set
of statements, whether an exception is thrown or not thrown.
try
{
//Statements that may
cause an exception.
}
catch
{
//Error handling code.
}
finally
{
//statements to be
executed.
}
class Num
{
int result;
Num()
{
result = 0;
}
public void Divide(int num1, int num2)
{
try
{
result = num1/num2;
}
catch(DivideByZeroException e)
{
Console.WriteLine(“Exception
catught {0}”,e);
}
finally
{
Console.WriteLine(“Result is
{0}”,result);
}
}
public static void Main(string[] args)
{
Num obj = new Num();
obj.Divide(10,0);
Console.ReadLine();
}
}
User-Defined
Exception
•
When
we want to catch an exception, do some work to handle the exception and then
pass the exception on to the calling code. Such kind of exceptions are known as
the user-defined exceptions.
•
The
Exception must be the base class for all the exceptions in C#.
User-Defined
exception classes are derived from the ApplicationException class.
Class CountZero
{
static void Main(string[] args)
{
Caclulate calc = new Calculate();
try
{
calc.DoAverage();
}
catch
{
Console.Writeline(“CountIsZeroException:
{0}”,e.Message);
}
Console.ReadLine();
}
}
public class
CountIsZeroException: ApplicationException
{
public CountIsZeroException(string message) :
base(message)
{
}
}
public class Calculate
{
int sume = 0, count=0;
float avaerage;
public void DoAverage()
{
if(count == 0)
throw(new CountIsZeroException(“Zero count is
doAverage”));
else
average = sum/count;
}
}
______________________________________________________________________
Reach us At: - 0120-4029000; 0120-4029024; 0120-4029025,
0120-4029027; 0120-4029029
Mbl: 9953584548
No comments:
Post a Comment