Anonymous user
Exceptions: Difference between revisions
m
Fixed lang tags.
m (Fixed lang tags.) |
|||
Line 6:
=={{header|Ada}}==
===Define an exception===
<lang ada>Foo_Error : exception;</lang>
===Raise an exception===
<lang ada>procedure Foo is
begin
raise Foo_Error;
end Foo;</lang>
Re-raising once caught exception:
<lang ada> ...
exception
when Foo_Error =>
if ... then -- Alas, cannot handle it here,
raise; -- continue propagation of
end if;</lang>
===Handle an exception===
<lang ada>procedure Call_Foo is
begin
Foo;
Line 35 ⟶ 28:
when others =>
... -- this catches all other exceptions
end Call_Foo;</lang>
===Ada.Exceptions===
The standard package Ada.Exceptions provides a possibility to attach messages to exceptions, to get exception occurrence information and textual description of exceptions. The following example illustrates basic functionality of:
<lang ada>with Ada.Exceptions; use Ada.Exceptions;
with Ada.Text_IO; use Ada.Text_IO;
Line 51 ⟶ 42:
when Error : others =>
Put_Line ("Something is wrong here" & Exception_Information (Error));
end Main;</lang>
=={{header|ALGOL 68}}==
===Define an exception===
<lang algol68># a user defined object #
MODE OBJECTFOO = STRUCT ( PROC (REF OBJECTFOO)BOOL foo event mended, ... );
PROC on foo event = (REF OBJECTFOO foo, PROC (REF OBJECTFOO)BOOL foo event)VOID: (
foo event mended OF foo := foo event
);</lang>
===Raise an exception===
<lang algol68>OBJECTFOO foo proxy := foo base; # event routines are specific to an foo #
on foo event(foo proxy, raise foo event);
Line 73 ⟶ 59:
# now raise example foo event #
IF NOT (foo event mended OF foo proxy)(foo proxy) THEN undefined # trace back # FI
OD;</lang>
Re-raising once caught exception:
<lang algol68>...
except foo event:
IF ... THEN # Alas, cannot handle it here continue propagation of #
IF NOT (foo event mended OF foo base)(foo base) THEN undefined # trace back # FI
FI</lang>
===Handle an exception===
<lang algol68>PROC raise foo event(REF OBJECTFOO foo)BOOL:
IF mend foo(foo) THEN
TRUE # continue #
Line 92 ⟶ 73:
except foo event
FALSE # OR fall back to default event routine #
FI</lang>
===Standard Prelude "on event" routines===
ALGOL 68 uses event routines extensively in the "standard transput"
Line 123 ⟶ 103:
=={{header|AppleScript}}==
===try===
<lang applescript>try
===try-on error===
<lang applescript>try
===error===
=={{header|AutoHotkey}}==
<lang AutoHotkey>foo()
If ErrorLevel
Msgbox calling foo failed with: %ErrorLevel%
Line 152 ⟶ 131:
ErrorLevel = foo_error
Return
}</lang>
=={{header|C}}==
Line 162 ⟶ 139:
===try-catch===
<lang c>
=={{header|C++}}==
Line 195 ⟶ 172:
===Defining exceptions===
<lang cpp>struct MyException
{
// data with info about exception
};</lang>
There's also a class <tt>std::exception</tt> which you can, but are not required to derive your exception class from. The advantage of doing so is that you can catch unknown exceptions and still get some meaningful information out. There are also more specific classes like <tt>std::runtime_error</tt> which derive from <tt>std::exception</tt>.
<lang cpp>#include <exception>
struct MyException: std::exception
{
char const* what() const throw() { return "description"; }
}</lang>
Note that in principle you can throw any copyable type as exception, including built-in types.
===Throw exceptions===
<lang cpp>// this function can throw any type of exception
void foo()
{
Line 232 ⟶ 204:
{
throw MyException();
}</lang>
===Catching exceptions===
<lang cpp>try {
}
catch (MyException &exc)
{
// handle exceptions of type MyException and derived
}
catch (std::exception &exc)
{
// handle exceptions derived from std::exception, which were not handled by above catches
// e.g.
std::cerr << exc.what() << std::endl;
}
catch (...)
{
// handle any type of exception not handled by above catches
}</lang>
=={{header|C sharp|C
{{works with|Visual Studio|2005}}
===Defining exceptions===
<lang
};</lang>
===Throw exceptions===
<lang
}</lang>
===Catching exceptions===
<lang
}</lang>
Line 289 ⟶ 258:
inside <cfscript>:
<lang
}</lang>
otherwise:
<lang
=={{header|Common Lisp}}==
Line 367 ⟶ 336:
<code>throw</code> is the built-in ''function'' which throws exceptions in the conventional sense: control goes to the <code>catch</code> block of the most recently entered <code>try</code>/<code>catch</code> construct.
<lang e>def nameOf(arg :int) {
if (arg == 43) {
return "Bob"
Line 417 ⟶ 385:
return ["notok", exceptionObj]
}
}</lang>
<lang e>? catching(42)
Line 453 ⟶ 420:
=={{header|Factor}}==
===Throw Exceptions===
Or a shorthand for this:
===Catch Exceptions===
<lang
=={{header|Forth}}==
Line 480 ⟶ 447:
===Throw Exceptions===
===Catch Exceptions===
test example. (Output shown in bold)
Note that CATCH only restores the stack pointers, not the stack values, so any values that were changed during the execution of the token will have undefined values. In practice, this means writing code to clean up the stack, like this:
=={{header|Haskell}}==
Line 503 ⟶ 470:
===Throw exceptions===
In the context of the IO monad, use "throwIO" to throw exceptions; the expression will return any type:
In purely functional context, use "throw" to throw exceptions; the expression will match any type:
To throw a user-defined exception, use "throwDyn":
===Catching exceptions===
The "catch" function performs the whole try-catch stuff. It is usually used in infix style:
pattern-matches on the exception type and argument:
<lang haskell>do
Note: Control.Exception's "catch" is different than Prelude's "catch".
To catch a user-defined exception, use "catchDyn":
<lang haskell>do
=={{header|J}}==
Line 538 ⟶ 505:
An exception in an explicit definition can be detected with <tt>try.</tt> and <tt>catcht.</tt> and can be thrown with <tt> throw. </tt> as seen below.
<lang j> pickyPicky =: verb define
=={{header|Java}}==
Line 561 ⟶ 528:
===Defining exceptions===
<lang
===Throw exceptions===
<lang
}</lang>
===Catching exceptions===
<lang
}</lang>
=={{header|JavaScript}}==
Line 599 ⟶ 566:
===Throwing exceptions===
}</lang>
===Catching exceptions===
}</lang>
=={{header|Logo}}==
{{works with|UCB Logo}}
There are also some predefined exceptions:
* '''throw "toplevel''' returns to the interactive prompt if uncaught (like control-C)
Line 636 ⟶ 603:
fail.mk
Using -@ to ignore the exception.
Line 643 ⟶ 610:
catch.mk
<lang
Using explicit exit 0 to ignore the exception.
Line 650 ⟶ 617:
catch.mk
<lang
=={{header|Modula-3}}==
Line 665 ⟶ 632:
...
RAISE EndOfFile;
...</lang>
===Catching exceptions===
Line 680 ⟶ 646:
FINALLY
CleanupFoo(); (* always executed *)
END;</lang>
Line 698 ⟶ 663:
MOO also has a finally statement:
<lang
this:foo();
finally
Line 744 ⟶ 709:
===Defining exceptions===
Like constructors, exceptions may or may not have an argument:
===Throw exceptions===
Throw exceptions with the "raise" function; the expression will match any type:
;;</lang>
===Catching exceptions===
The "with" syntax pattern-matches on the exception type and argument:
<lang ocaml>try
=={{header|Perl}}==
Line 787 ⟶ 752:
Exceptions were not available prior to PHP 5.0
===Define exceptions===
}</lang>
===Throwing exceptions===
}</lang>
===Catching Exceptions===
}</lang>
=={{header|Pop11}}==
Line 808 ⟶ 773:
===Throwing exceptions===
<lang pop11>define throw_exception();
throw([my_exception my_data]);
enddefine;</lang>
===Catching exceptions===
<lang pop11>define main();
vars cargo;
define catcher();
Line 826 ⟶ 788:
enddefine;
main();</lang>
=={{header|Python}}==
Line 833 ⟶ 794:
===Defining an exception===
<lang python>
Note: In most cases new exceptions are defined simply using the ''pass'' statement. For example:
<lang python>class MyInvalidArgument(ValueError):
pass</lang>
This example makes "MyInvalidArgument" an type of ValueError (one of the built-in exceptions). It's simply declared as a subclass of the existing exception and no over-riding is necessary. (An except clause for ValueError would catch MyInvalidArgument exceptions ... but one's code could insert a more specific exception handler for the more specific type of exception).
Line 896 ⟶ 855:
=={{header|Raven}}==
=={{header|Ruby}}==
Line 951 ⟶ 910:
Ruby has a separate exception-like system that is meant to be used to exit out of deep executions that are not errors.
<lang
throw :done
end
Line 965 ⟶ 924:
===Handling Exceptions===
[
block handlingCases: {Abort -> [| :_ | ^ Nil]}
].</lang>
===Define Exceptions===
"An Abort is a Restart which exits the computation, unwinding the stack."
Line 993 ⟶ 948:
[
c tryHandlers
].</lang>
===Throwing Exceptions===
<lang slate>(fileName endsWith: '.image') ifTrue: [error: 'Image filename specified where Slate source expected. Make sure you run slate with the -i flag to specify an image.'].</lang>
=={{header|Standard ML}}==
===Define Exceptions===
<lang
===Throw Exceptions===
<lang
===Catch Exceptions===
<lang
=={{header|Tcl}}==
Line 1,063 ⟶ 1,013:
throwing exceptions
].</lang>
catching them
=={{header|Visual Basic .NET}}==
===Defining exceptions===
<lang
===Throw exceptions===
<lang
===Catching exceptions===
<lang
{{omit from|M4}}
|