Exceptions: Difference between revisions

no edit summary
mNo edit summary
No edit summary
Line 4:
=={{header|Ada}}==
===Define an exception===
<lang ada>
Foo_Error : exception;
</adalang>
===Raise an exception===
<lang ada>
procedure Foo is
begin
raise Foo_Error;
end Foo;
</adalang>
Re-raising once caught exception:
<lang ada>
...
exception
Line 22:
raise; -- continue propagation of
end if;
</adalang>
===Handle an exception===
<lang ada>
procedure Call_Foo is
begin
Line 34:
... -- this catches all other exceptions
end Call_Foo;
</adalang>
===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 50:
Put_Line ("Something is wrong here" & Exception_Information (Error));
end Main;
</adalang>
=={{header|ALGOL 68}}==
===Define an exception===
Line 177:
 
===Defining exceptions===
<lang cpp>
struct MyException
{
// data with info about exception
};
</cpplang>
 
There's also a class <codett>std::exception</codett> 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 <codett>std::runtime_error</codett> which derive from <codett>std::exception</codett>.
 
<lang cpp>
#include <exception>
struct MyException: std::exception
Line 192:
char const* what() const throw() { return "description"; }
}
</cpplang>
 
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 215:
throw MyException();
}
</cpplang>
 
===Catching exceptions===
<lang cpp>
try {
foo();
Line 236:
// handle any type of exception not handled by above catches
}
</cpplang>
 
=={{header|C sharp|C #}}==
Line 327:
 
=={{header|Haskell}}==
Exceptions can be implemented using monads; no special syntax is necessary.[http://haskell.org/haskellwiki/Exception] In GHC, specialized functionality for exceptions are provided by the <codett>Control.Exception</codett> module.
 
===Defining exceptions===
Line 362:
 
=={{header|J}}==
Program<codett> u :: v </codett>executes <codett>u</codett> and provides its result as output unless an error occurs. In case of error, the result of <codett>v</codett> is provided instead.<br>
 
An exception in an explicit definition can be detected with <codett>try.</codett> and <codett>catcht.</codett> and can be thrown with <codett> throw. </codett> as seen below.
 
pickyPicky =: verb define
Line 610:
===Defining an exception===
 
<lang python> import exceptions
class SillyError(exceptions.Exception):
def __init__(self,args=None):
self.args=args</pythonlang>
 
Note: In most cases new exceptions are defined simply using the ''pass'' statement. For example:
 
<lang python>
class MyInvalidArgument(ValueError):
pass
</pythonlang>
 
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 626:
===Throwing an exception===
 
<lang python>def spam():
raise SillyError, 'egg'</pythonlang>
 
===Handling an exception===
Line 634:
try-except-finally-else
 
<lang python> try:
foo()
except SillyError, se:
Line 643:
else:
# no exception occurred
quux()</pythonlang>
 
Before Python 2.5 it was not possible to use finally and except together. (It was necessary to nest a separate ''try''...''except'' block inside of your ''try''...''finally'' block).
Line 666:
===Defining an exception===
 
<lang ruby>class SillyError < Exception
def initialize(args = nil)
@args = args
end
end</rubylang>
 
Note: Often new exceptions are defined simply with no body. For example:
 
<lang ruby>class MyInvalidArgument < ArgumentError
end</rubylang>
 
This example makes "MyInvalidArgument" an type of ArgumentError (one of the built-in exceptions). It's simply declared as a subclass of the existing exception and no over-riding is necessary. (A rescue clause for ArgumentError would catch MyInvalidArgument exceptions ... but one's code could insert a more specific exception handler for the more specific type of exception).
Line 681:
===Throwing an exception===
 
<lang ruby>def spam
raise SillyError, 'egg'
end</rubylang>
 
===Handling an exception===
Line 689:
rescue/else/ensure
 
<lang ruby>begin
foo
rescue SillyError, se
Line 699:
ensure
baz
end</rubylang>
 
The "rescue" clause is like the "catch" clause in other languages. The "ensure" clause is like the "finally" clause in other languages.
Line 706:
Ruby has a separate exception-like system that is meant to be used to exit out of deep executions that are not errors.
 
<lang python>def foo
throw :done
end
Line 712:
catch :done do
foo
end</pythonlang>
 
You can only "throw" and "catch" symbols. Like exceptions, the throw can be made from a function defined elsewhere from the catch block.