Exceptions/Catch an exception thrown in a nested call: Difference between revisions

Content added Content deleted
m (change include to be prelude)
(C# implementation)
Line 171:
</pre>
The exact behavior for an uncaught exception is implementation-defined.
 
=={{header|C sharp|C#}}==
This example will first catch U0 and print "U0 Caught" to the console when it does. The uncaught U1 exception will then cause the program to terminate and print the type of the exception, location of the error, and the stack.
 
<lang csharp>
using System; //Used for Exception and Console classes
class Exceptions
{
class U0 : Exception { }
class U1 : Exception { }
static int i;
static void foo()
{
for (i = 0; i < 2; i++)
try
{
bar();
}
catch (U0) {
Console.WriteLine("U0 Caught");
}
}
static void bar()
{
baz();
}
static void baz(){
if (i == 0)
throw new U0();
throw new U1();
}
 
public static void Main()
{
foo();
}
}
</lang>
 
Output:
<pre>
U0 Caught
Unhandled Exception: Exceptions+U1: Exception of type 'Exceptions+U1' was thrown.
at Exceptions.baz() in Program.cs:line 27
at Exceptions.bar() in Program.cs:line 22
at Exceptions.foo() in Program.cs:line 14
at Exceptions.Main() in Program.cs:line 32
</pre>
 
 
=={{header|D}}==
First exception will be caught and message will be displayed, second will be caught by default exception handler.