Information Technology Reference
In-Depth Information
Throwing Without an Exception Object
The throw statement can also be used without an exception object, inside a catch block.
￿
This form rethrows the current exception, and the system continues its search for addi-
tional handlers for it.
This form can only be used inside a catch statement.
￿
For example, the following code rethrows the exception from inside the first catch clause:
class MyClass
{
public static void PrintArg(string arg)
{
try
{
try
{
if (arg == null)
{
ArgumentNullException MyEx = new ArgumentNullException();
throw MyEx;
}
Console.WriteLine(arg);
}
catch (ArgumentNullException e)
{
Console.WriteLine("Inner Catch: {0}", e.Message);
throw;
}
} Rethrow the exception—no additional parameters
catch
{
Console.WriteLine("Outer Catch: Handling an Exception.");
}
}
}
class Program {
static void Main() {
string s = null;
MyClass.PrintArg(s);
}
}
Search WWH ::




Custom Search