Jump to content

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

m
Line 1:
{{task}}
{{omit from|C}}
Show how to create a user-defined exception and show how to catch an exception raised from several nested calls away.
{{omit from|GUISS}}
{{omit from|M4}}
{{omit from|Retro}}
 
Show how to create a user-defined exception and
show how to catch an exception raised from several nested calls away.
 
# Create two user-defined exceptions, U0 and U1.
Line 41 ⟶ 47:
end loop;
end Exceptions_From_Nested_Calls;</lang>
{{out}}
Sample output:
<pre>
Procedure Foo caught exception U0
Line 47 ⟶ 53:
raised EXCEPTIONS_FROM_NESTED_CALLS.U1 : exceptions_from_nested_calls.adb:13
</pre>
An unhandled exception leads to termination of the corresponding [[task]]. When the task is the main task of the program as in the example, the whole program is terminated. In the example the exception back tracing message is compiler-specific (in this case it is [[GNAT]] and further depends on the compiler options.
When the task is the main task of the program as in the example, the whole program is terminated. In the example the exception back tracing message is compiler-specific
(in this case it is [[GNAT]] and further depends on the compiler options.
 
=={{header|Aime}}==
Line 167 ⟶ 175:
 
foo</lang>
{{out}}
Output:
<pre>
Function foo caught exception u0
Line 291 ⟶ 299:
ENDPROC
</lang>
Output{{out}} (the second message is output by the default error handler):
<pre>Exception U0 caught in foo
 
Line 435 ⟶ 443:
 
</lang>
{{out}}
Output:
<langpre>Foo entering bar.
Bar door is open.
Bar door is closed.
Line 445 ⟶ 453:
Bar door is closed.
*** Error: U1 Bartender Error. Bartender kicked customer out of bar Baz.
</langpre>
 
=={{header|C++}}==
First exception will be caught and message will be displayed, second will be caught by the default exception handler, which as required by the C++ Standard, will call terminate(), aborting the task,
second will be caught by the default exception handler,
typically with an error message.
which as required by the C++ Standard, will call terminate(),
aborting the task, typically with an error message.
 
<lang cpp>#include <iostream>
Line 523 ⟶ 533:
}</lang>
 
{{out}}
Output:
<pre>
U0 Caught
Line 555 ⟶ 565:
(foo)</lang>
 
Sample output{{out}} (the numbered lines are output from <code>trace</code>):
 
<lang lisp> 0: (FOO)
1: (BAR USER-CONDITION-1)
Line 564 ⟶ 573:
2: (BAZ USER-CONDITION-2)</lang>
 
At this point, the debugger (if any) is invoked with the unhandled condition of type USER-CONDITION-2.
with the unhandled condition of type USER-CONDITION-2.
 
=={{header|D}}==
First exception will be caught and message will be displayed, second will be caught by default exception handler.
second will be caught by default exception handler.
<lang d>class U0 : Exception {
this() @safe pure nothrow { super("U0 error message"); }
Line 599 ⟶ 610:
foo;
}</lang>
{{outputout}}
<pre>test.U1(at)test.d(8): U1 error message
----------------
Line 654 ⟶ 665:
end.</lang>
 
{{out}}
Output:
<pre>
Exception U0 caught
Line 742 ⟶ 753:
end</lang>
 
{{out}}
Sample output:
<pre>Caught U0 exception.
Won't catch U1 exception...
Line 808 ⟶ 819:
end</lang>
 
{{out}}
Output:
<pre>
Caught a U0 with message: 'This is the U0 exception'
Line 900 ⟶ 911:
</lang>
 
{{out}}
Output:
 
<pre>
Caught U0
Line 924 ⟶ 934:
</pre>
 
The output shows the first exception is caught and handled. The second exception is not handled, and results in the program finishing and printing a stack trace.
The second exception is not handled, and results in the program finishing
and printing a stack trace.
 
=={{header|Go}}==
Not strictly conforming to task description as foo does not directly call bar.
 
The panic/recover mechanism of Go is missing (by design)
The panic/recover mechanism of Go is missing (by design) some elements of exception handling needed for this task. Specifically, a function that recovers a panic cannot resume execution of the remainder of the function. If foo recovers a panic in the first call to bar, there is no way for it to make the second call to bar. The solution here is to define a wrapper, or proxy function, called try. Function foo calls bar indirectly through try.
some elements of exception handling needed for this task.
Specifically, a function that recovers a panic cannot resume
execution of the remainder of the function.
If foo recovers a panic in the first call to bar, there is no way for it
to make the second call to bar.
The solution here is to define a wrapper, or proxy function, called try.
Function foo calls bar indirectly through try.
<lang go>// Outline for a try/catch-like exception mechanism in Go
//
Line 1,179 ⟶ 1,198:
Right v -> putStrLn ("Return value: " ++ show v)</lang>
 
{{out}}
The output of this program is:
<pre>
 
foo
bar
Line 1,188 ⟶ 1,207:
baz
Caught error at top level: U1
</pre>
 
==Icon and {{header|Unicon}}==
 
The following Unicon example makes use of support for exceptions found in the
in the [http://tapestry.tucson.az.us/unilib/ The Unicon Code Library]. <i>Since
<i>Since exception support is not built into Unicon, but rather implemented as
but rather implemented as Unicon code, there are limitations not found in languages that natively
not found in languages that natively support exceptions.</i>
 
<lang Unicon>import Exceptions
Line 1,243 ⟶ 1,263:
end</lang>
 
{{out}}
When run, this example produces:
<pre>
U0: First exception:
Line 1,256 ⟶ 1,276:
</pre>
 
Note: it may be possible to implement exceptions in Icon; however, it would require a major rework and would likely be inelegant.
it would require a major rework and would likely be inelegant.
 
=={{header|J}}==
Line 1,298 ⟶ 1,319:
 
=={{header|Java}}==
Methods that may throw an exception (or that call a method
Methods that may throw an exception (or that call a method that may throw an exception that it does not catch) must explicitly declare that they can throw such an exception (or a superclass thereof), unless they are unchecked exceptions (subclasses of <code>RuntimeException</code> or <code>Error</code>):
that may throw an exception that it does not catch)
must explicitly declare that they can throw such an exception
(or a superclass thereof), unless they are unchecked exceptions
(subclasses of <code>RuntimeException</code> or <code>Error</code>):
<lang java>class U0 extends Exception { }
class U1 extends Exception { }
Line 1,328 ⟶ 1,353:
}
}</lang>
{{out}}
Sample output:
<pre>
Function foo caught exception U0
Line 1,337 ⟶ 1,362:
at ExceptionsTest.main(ExceptionsTest.java:27)
</pre>
The first line of the output is generated from catching the U0 exception in function foo.
in function foo.
 
Uncaught exceptions give information showing where the exception originated through the nested function calls together with the name of the uncaught exception, (U1) to stderr, then quit the running program.
originated through the nested function calls together with the name
of the uncaught exception, (U1) to stderr,
then quit the running program.
 
=={{header|JavaScript}}==
Line 1,385 ⟶ 1,414:
 
foo();</lang>
{{out}} from [[Rhino]] output:
<pre>caught exception U0
js: "nested_calls.js", line 31: exception from uncaught JavaScript throw: U1</pre>
{{out}} from [[SpiderMonkey]] output:
<pre>caught exception U0
uncaught exception: U1</pre>
 
=={{header|Lasso}}==
Lasso currently does not currently have a try mechanic — but we can easily add one like so.
but we can easily add one like so.
 
<lang Lasso>define try(exception) => {
Line 1,609 ⟶ 1,639:
}
}</lang>
{{out}}
Output:
<pre>Exception U0 caught.
 
Line 1,636 ⟶ 1,666:
 
foo()</lang>
{{out}}
Output:
<pre>Function foo caught exception U0
Traceback (most recent call last)
Line 1,690 ⟶ 1,720:
return 0;
}</lang>
{{out}}
Sample output:
<pre>
2011-06-03 23:11:53.871 Untitled[9968:903] Function foo caught exception U0
Line 1,746 ⟶ 1,776:
{Foo}</lang>
 
{{out}}
Sample output:
<pre>
Procedure Foo caught exception u0
Line 1,781 ⟶ 1,811:
 
foo();</lang>
{{out}}
Sample output:
<pre>
Function foo caught exception U0
Line 1,826 ⟶ 1,856:
(mapc trace '(foo bar baz))
(foo)</lang>
{{out}}
Output:
<pre> foo :
bar : U0
Line 1,898 ⟶ 1,928:
 
=={{header|Python}}==
There is no extra syntax to add to functions and/or methods such as ''bar'', to say what exceptions they may raise or pass through them:
to say what exceptions they may raise or pass through them:
<lang python>class U0(Exception): pass
class U1(Exception): pass
Line 1,916 ⟶ 1,947:
 
foo()</lang>
{{out}}
Sample output:
<pre>
Function foo caught exception U0
Line 1,931 ⟶ 1,962:
U1
</pre>
The first line of the output is generated from catching the U0 exception in function foo.
in function foo.
 
Uncaught exceptions give information showing where the exception originated through the nested function calls together with the name of the uncaught exception, (U1) to stderr, then quit the running program.
through the nested function calls together with the name of the
uncaught exception, (U1) to stderr, then quit the running program.
 
=={{header|R}}==
The counter for the number of calls to baz is kept in the global environment for simplicity, but you could hide it in your own environment. See ?new.env and ?get.
the global environment for simplicity, but you could hide it
in your own environment.
See ?new.env and ?get.
<lang r>
number_of_calls_to_baz <- 0
Line 1,959 ⟶ 1,996:
traceback()
</lang>
{{out}}
<pre>
6: stop(e) at file.r#11
Line 1,990 ⟶ 2,028:
(foo)
</lang>
{{out}}
Output:
<lang racket>
Function foo caught exception U0
Line 1,997 ⟶ 2,035:
 
=={{header|REXX}}==
While the REXX language doesn't have a ''throw'' capability ''pe se'', it does have the ability to catch exceptions (by label).
it does have the ability to catch exceptions (by label).
<br>This type of exception handling (in REXX) has its limitation (the label is local to the program, not external subroutines).
<br>This type of exception handling (in REXX) has its limitation
(the label is local to the program, not external subroutines).
<lang rexx>/*REXX program to create two exceptions & demonstrate how to handle them*/
call foo /*invoke the FOO function. */
Line 2,022 ⟶ 2,062:
U0: return -1 /*handle exception if not caught.*/
U1: return -1 /* " " " " " */</lang>
{{out}}
'''output'''
<pre style="overflow:scroll">
exception U0 caught in FOO
Line 2,054 ⟶ 2,094:
foo
foo </lang>
The first call to foo causes the U0 exception. It gets rescued. The second call results in a U1 exception which is not rescued, so the program dumps a stacktrace and exits.
The second call results in a U1 exception which is not rescued,
so the program dumps a stacktrace and exits.
{{out}}
<pre>$ ruby test2.rb
Line 2,065 ⟶ 2,107:
 
=={{header|Scala}}==
{{libheader|Scala}}
[[Category:Scala Implementations]]{{libheader|Scala}}<lang Scala>object ExceptionsTest extends App {
<lang Scala>object ExceptionsTest extends App {
class U0 extends Exception
class U1 extends Exception
Line 2,084 ⟶ 2,127:
foo
}
</lang>
</lang>Exception U0 is caught, exception U1 is caught and re-thrown. Program execution is terminated as the U1 exception is not caught when thrown the second time.
Exception U0 is caught, exception U1 is caught and re-thrown.
Program execution is terminated as the U1 exception is not caught
when thrown the second time.
 
=={{header|Seed7}}==
When an [http://seed7.sourceforge.net/manual/errors.htm#Exceptions exception] is not [http://seed7.sourceforge.net/manual/errors.htm#Handlers handled]
is not [http://seed7.sourceforge.net/manual/errors.htm#Handlers handled]
the program is terminated and a [http://seed7.sourceforge.net/manual/errors.htm#Stack_trace stack trace] is written.
<lang seed7>$ include "seed7_05.s7i";
Line 2,142 ⟶ 2,189:
 
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}functional code, not class based, using blocks as functions, and anonymous exceptions (signals):
using blocks as functions, and anonymous exceptions (signals):
<lang Smalltalk>| u0 u1 foo bar baz|
 
Line 2,173 ⟶ 2,221:
 
foo value</lang>
"traditional" implementation, using class based exceptions, and method invocations:
and method invocations:
<lang Smalltalk>Exception
subclass: #U0
Line 2,248 ⟶ 2,297:
foo
foo</lang>
{{out}}
Running this program results in:
<pre>$ tclsh85 exceptions.tcl
caught exception U0
Line 2,293 ⟶ 2,342:
@(foo)</lang>
 
{{out|Run:}}
<pre>$ txr except.txr
caught u0: text0
Line 2,302 ⟶ 2,351:
 
=={{header|Ursala}}==
Foo calls bar, and bar calls baz. Normal termination of bar is bypassed if
if baz raises an exception. The exception is caught or not by foo.
The exception is caught or not by foo.
<lang Ursala>#import std
 
Line 2,325 ⟶ 2,375:
Note that the definition of bar includes no conditional (?) or exception
handling operators, and is written without regard for any exceptions.
Here is an example bash session.:
<pre>
$ echo "valid input" | foo
Line 2,359 ⟶ 2,409:
Exception thrown: U1(An Exception)
</pre>
foo catches exception U0 and ignores it.
foo catches exception U0 and ignores it. It calls bar with the exception to throw. bar in turn calls baz with that exception, which it throws. A stack trace is printed when an uncaught exception bubbles up to the VM (which handles all catchable exceptions).
It calls bar with the exception to throw. bar in turn calls baz
{{omit from|C}}
with that exception, which it throws.
{{omit from|GUISS}}
A stack trace is printed when an uncaught exception bubbles up to the VM
{{omit from|M4}}
(which handles all catchable exceptions).
{{omit from|Retro}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.