Anonymous user
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;
</
===Raise an exception===
<lang ada>
procedure Foo is
begin
raise Foo_Error;
end Foo;
</
Re-raising once caught exception:
<lang ada>
...
exception
Line 22:
raise; -- continue propagation of
end if;
</
===Handle an exception===
<lang ada>
procedure Call_Foo is
begin
Line 34:
... -- this catches all other exceptions
end Call_Foo;
</
===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;
</
=={{header|ALGOL 68}}==
===Define an exception===
Line 177:
===Defining exceptions===
<lang cpp>
struct MyException
{
// data with info about exception
};
</
There's also a class <
<lang cpp>
#include <exception>
struct MyException: std::exception
Line 192:
char const* what() const throw() { return "description"; }
}
</
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();
}
</
===Catching exceptions===
<lang cpp>
try {
foo();
Line 236:
// handle any type of exception not handled by above catches
}
</
=={{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 <
===Defining exceptions===
Line 362:
=={{header|J}}==
Program<
An exception in an explicit definition can be detected with <
pickyPicky =: verb define
Line 610:
===Defining an exception===
<lang python> import exceptions
class SillyError(exceptions.Exception):
def __init__(self,args=None):
self.args=args</
Note: In most cases new exceptions are defined simply using the ''pass'' statement. For example:
<lang python>
class MyInvalidArgument(ValueError):
pass
</
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'</
===Handling an exception===
Line 634:
try-except-finally-else
<lang python> try:
foo()
except SillyError, se:
Line 643:
else:
# no exception occurred
quux()</
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</
Note: Often new exceptions are defined simply with no body. For example:
<lang ruby>class MyInvalidArgument < ArgumentError
end</
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</
===Handling an exception===
Line 689:
rescue/else/ensure
<lang ruby>begin
foo
rescue SillyError, se
Line 699:
ensure
baz
end</
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</
You can only "throw" and "catch" symbols. Like exceptions, the throw can be made from a function defined elsewhere from the catch block.
|