Consider the following C# code that catches exceptions:
try {
throw new System.IO.FileNotFoundException("File missing");
} catch (System.Exception ex) {
Console.WriteLine(ex.GetType().Name);
}What will be printed?
try { throw new System.IO.FileNotFoundException("File missing"); } catch (System.Exception ex) { Console.WriteLine(ex.GetType().Name); }
FileNotFoundException inherits from IOException, which inherits from Exception.
The thrown exception is of type FileNotFoundException. The catch block catches it as Exception, but ex.GetType().Name returns the actual runtime type, which is FileNotFoundException.
Given this code snippet:
try {
throw new System.ArgumentNullException("param");
} catch (System.SystemException ex) {
Console.WriteLine(ex.GetType().Name);
}What will be printed?
try { throw new System.ArgumentNullException("param"); } catch (System.SystemException ex) { Console.WriteLine(ex.GetType().Name); }
ArgumentNullException inherits from SystemException.
The thrown exception is ArgumentNullException, which inherits from SystemException. The catch block catches SystemException, so it catches the thrown exception. The ex.GetType().Name returns the actual type ArgumentNullException.
Examine this code snippet:
try {
throw new System.Exception("Error");
} catch (System.IO.IOException ex) {
Console.WriteLine("IO error");
} catch (System.Exception ex) {
Console.WriteLine("General error");
}What will be printed?
try { throw new System.Exception("Error"); } catch (System.IO.IOException ex) { Console.WriteLine("IO error"); } catch (System.Exception ex) { Console.WriteLine("General error"); }
Exception is not an IOException, but it is an Exception.
The thrown exception is Exception. The first catch block for IOException does not match. The second catch block for Exception matches and prints General error.
Look at this code:
try {
try {
throw new System.IndexOutOfRangeException("Index error");
} catch (System.Exception ex) {
throw new System.ApplicationException("App error", ex);
}
} catch (System.ApplicationException ex) {
Console.WriteLine(ex.InnerException.GetType().Name);
}What will be printed?
try { try { throw new System.IndexOutOfRangeException("Index error"); } catch (System.Exception ex) { throw new System.ApplicationException("App error", ex); } } catch (System.ApplicationException ex) { Console.WriteLine(ex.InnerException.GetType().Name); }
The inner exception is the original exception thrown inside the inner try block.
The inner try throws IndexOutOfRangeException. It is caught and wrapped inside an ApplicationException. The outer catch prints the type of the inner exception, which is IndexOutOfRangeException.
In the .NET exception hierarchy, which class is the ultimate base class for all exceptions thrown by the Common Language Runtime (CLR)?
Think about the root class that all exceptions inherit from.
System.Exception is the base class for all exceptions in .NET. System.SystemException and System.ApplicationException derive from it. System.Object is the base of all classes but not specifically for exceptions.