Exception Handling:
• Exceptions are raised by the runtime when
an
error occurs.
• If we do not handle the exception, the
running program will be terminated.
• Exceptions can
provide full
information about the error so that it might be recovered.
Exception Examples:
All the exceptions are derived from
Exception
class present in System
namespace. Some of them are as follows:
• IndexOutOfRangeException
• DivideByZeroException
• FormatException
• FileNotFoundException
• UnauthorizedAccessException
• NotSupportedException
IndexOutOfRangeException:
using System;
class Program
{
static void
Main(string[] args)
{
int[] numbers = new int[3];
numbers[5] = 15; // Index number 5 doesnot exists
}
}
DivideByZeroException:
using System;
class Program
{
static void
Main(string[] args)
{
int k = 0;
k = k / k; //k is 0
}
}
FormatException:
using System;
class Program
{
static void
Main(string[] args)
{
Console.WriteLine(Convert.ToInt32("xyz")); //xyz cannot be converted to int32
}
}
FileNotFoundException:
using System;
using System.IO;
class Program
{
static void
Main(string[] args)
{
FileStream fs = new FileStream("NonExistingFile", FileMode.Open, FileAccess.Read);
}
}
UnauthorizedAccessException:
using System;
using System.IO;
class Program
{
static void
Main(string[] args)
{
FileStream fs = new FileStream("ReadOnlyFile",FileMode.Open,FileAccess.Write);
}
}
NotSupportedException:
using System;
using System.IO;
class Program
{
static void
Main(string[] args)
{
FileStream fs = new FileStream("ReadOnlyFile.txt",FileMode.Open,FileAccess.Read);
fs.WriteByte(128);
}
}
Try-catch-finally flow chart:
Exception Overview:
• Keywords includes:
–throw
–try
–catch
–finally
• Exceptions are
types that all ultimately derive from System.Exception.
• Use a try block around the statements
that might throw exceptions.
• Once an exception occurs in the try
block, the flow of control jumps to the first associated exception handler that
is present anywhere in the call stack. In C#, the catch keyword is used to
define an exception handler.
• If no exception handler for a given
exception is present, the program stops executing with an error message.
• Do not catch an exception unless you
can handle it and leave the application in a known state. If you catch System.Exception,
rethrow
it using the throw keyword at the end of the catch block.
• If a catch block defines an exception
variable, you can use it to obtain more information about the type of exception
that occurred.
• Exceptions can be explicitly generated by
a program by using the throw
keyword.
• Exception objects contain detailed
information about the error, such as the state of the call stack and a text
description of the error.
• Code in a finally block is executed even
if an exception is thrown. Use a finally block to release resources, for
example to close any streams or files that were opened in the try block.
Example:
class Program
{
static void Main(string[] args)
{
try
{
int k = 0;
int a = 5 / k; // exception occur
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Output:
Thank You for Reading, Like this if you understood, It helps a lot.