Term cloud

Filtering exceptions

Filtering exceptions is a process that handles multiple exception by using multiple exception classes, allows you to respond differently to different types of errors.

Ex:
C#
try
{
  StreamReader sr = new StreamReader("example.txt");
  Console.WriteLine(sr.ReadToEnd());
}
catch (System.IO.FileNotFoundException ex)
{
  Console.WriteLine("The file could not be found.");
}
catch (System.UnauthorizedAccessException ex)
{
  Console.WriteLine("You do not have sufficient permissions.");
}
catch (Exception ex)
{
  Console.WriteLine("Error reading file: " + ex.Message);
}

' VB
Try
Dim sr As StreamReader = New StreamReader("example.txt")
  Console.WriteLine(sr.ReadToEnd)
Catch ex As System.IO.FileNotFoundException
  Console.WriteLine("The file could not be found.")
Catch ex As System.UnauthorizedAccessException
  Console.WriteLine("You do not have sufficient permissions.")
Catch ex As Exception
  Console.WriteLine("Error reading file: " + ex.Message)
End Try

0 comments:

Post a Comment

 

Dot net terms Copyright © 2010 | Designed by Ipietoon for Free Blogger Template