Notes - Exceptions and File Processing Notes | EduRev

: Notes - Exceptions and File Processing Notes | EduRev

 Page 1


Exceptions and File 
Processing
Page 2


Exceptions and File 
Processing
Exceptions
On occasion a segment of code in a program may 
result in an error condition, such as an attempt to 
divide by zero, an array index out of bounds, failure 
to open a file when requested, etc.
Careful program design can help minimize the number 
of exceptions that occur; however, occasionally they 
do happen.
In this material we will show how you can handle 
exceptions when they occur, rather than letting the 
program just terminate.  This is done with try and 
catch.
Page 3


Exceptions and File 
Processing
Exceptions
On occasion a segment of code in a program may 
result in an error condition, such as an attempt to 
divide by zero, an array index out of bounds, failure 
to open a file when requested, etc.
Careful program design can help minimize the number 
of exceptions that occur; however, occasionally they 
do happen.
In this material we will show how you can handle 
exceptions when they occur, rather than letting the 
program just terminate.  This is done with try and 
catch.
try
Suppose a program segment has a chance of 
causing an exception. That code can be 
placed in a try segment.
try
{
//code that might cause the error.
}
Page 4


Exceptions and File 
Processing
Exceptions
On occasion a segment of code in a program may 
result in an error condition, such as an attempt to 
divide by zero, an array index out of bounds, failure 
to open a file when requested, etc.
Careful program design can help minimize the number 
of exceptions that occur; however, occasionally they 
do happen.
In this material we will show how you can handle 
exceptions when they occur, rather than letting the 
program just terminate.  This is done with try and 
catch.
try
Suppose a program segment has a chance of 
causing an exception. That code can be 
placed in a try segment.
try
{
//code that might cause the error.
}
catch
If the code in the try does indeed result in an 
error, an exception is thrown, which can then 
be handled in a catch.
catch
{
//process the error.
}
Page 5


Exceptions and File 
Processing
Exceptions
On occasion a segment of code in a program may 
result in an error condition, such as an attempt to 
divide by zero, an array index out of bounds, failure 
to open a file when requested, etc.
Careful program design can help minimize the number 
of exceptions that occur; however, occasionally they 
do happen.
In this material we will show how you can handle 
exceptions when they occur, rather than letting the 
program just terminate.  This is done with try and 
catch.
try
Suppose a program segment has a chance of 
causing an exception. That code can be 
placed in a try segment.
try
{
//code that might cause the error.
}
catch
If the code in the try does indeed result in an 
error, an exception is thrown, which can then 
be handled in a catch.
catch
{
//process the error.
}
while (M!=0 || N!=0)
{
Console.WriteLine("Enter two integers.");
try
{ Console.Write("First Integer: ");
M = Int32.Parse(Console.ReadLine());
Console.Write("Second Integer: ");
N = Int32.Parse(Console.ReadLine());
Console.WriteLine("The quotient is {0}.", M / N);
}
catch (DivideByZeroException)
{  Console.WriteLine("Attempt to divide by zero");
continue;
}
catch (FormatException)
{
Console.WriteLine("Both must be integers:");
continue;
}
}
Read More
Offer running on EduRev: Apply code STAYHOME200 to get INR 200 off on our premium plan EduRev Infinity!