CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
4
Task/Exceptions/0DESCRIPTION
Normal file
4
Task/Exceptions/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{{Control Structures}}
|
||||
This task is to give an example of an exception handling routine and to "throw" a new exception.
|
||||
|
||||
Cf. [[Exceptions Through Nested Calls]]
|
||||
2
Task/Exceptions/1META.yaml
Normal file
2
Task/Exceptions/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Control Structures
|
||||
6
Task/Exceptions/ALGOL-68/exceptions-1.alg
Normal file
6
Task/Exceptions/ALGOL-68/exceptions-1.alg
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# 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
|
||||
);
|
||||
8
Task/Exceptions/ALGOL-68/exceptions-2.alg
Normal file
8
Task/Exceptions/ALGOL-68/exceptions-2.alg
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
OBJECTFOO foo proxy := foo base; # event routines are specific to an foo #
|
||||
|
||||
on foo event(foo proxy, raise foo event);
|
||||
|
||||
WHILE TRUE DO
|
||||
# now raise example foo event #
|
||||
IF NOT (foo event mended OF foo proxy)(foo proxy) THEN undefined # trace back # FI
|
||||
OD;
|
||||
5
Task/Exceptions/ALGOL-68/exceptions-3.alg
Normal file
5
Task/Exceptions/ALGOL-68/exceptions-3.alg
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
...
|
||||
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
|
||||
7
Task/Exceptions/ALGOL-68/exceptions-4.alg
Normal file
7
Task/Exceptions/ALGOL-68/exceptions-4.alg
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
PROC raise foo event(REF OBJECTFOO foo)BOOL:
|
||||
IF mend foo(foo) THEN
|
||||
TRUE # continue #
|
||||
ELSE
|
||||
except foo event
|
||||
FALSE # OR fall back to default event routine #
|
||||
FI
|
||||
1
Task/Exceptions/Ada/exceptions-1.ada
Normal file
1
Task/Exceptions/Ada/exceptions-1.ada
Normal file
|
|
@ -0,0 +1 @@
|
|||
Foo_Error : exception;
|
||||
4
Task/Exceptions/Ada/exceptions-2.ada
Normal file
4
Task/Exceptions/Ada/exceptions-2.ada
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
procedure Foo is
|
||||
begin
|
||||
raise Foo_Error;
|
||||
end Foo;
|
||||
6
Task/Exceptions/Ada/exceptions-3.ada
Normal file
6
Task/Exceptions/Ada/exceptions-3.ada
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
...
|
||||
exception
|
||||
when Foo_Error =>
|
||||
if ... then -- Alas, cannot handle it here,
|
||||
raise; -- continue propagation of
|
||||
end if;
|
||||
9
Task/Exceptions/Ada/exceptions-4.ada
Normal file
9
Task/Exceptions/Ada/exceptions-4.ada
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure Call_Foo is
|
||||
begin
|
||||
Foo;
|
||||
exception
|
||||
when Foo_Error =>
|
||||
... -- do something
|
||||
when others =>
|
||||
... -- this catches all other exceptions
|
||||
end Call_Foo;
|
||||
12
Task/Exceptions/Ada/exceptions-5.ada
Normal file
12
Task/Exceptions/Ada/exceptions-5.ada
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
with Ada.Exceptions; use Ada.Exceptions;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Main is
|
||||
begin
|
||||
...
|
||||
Raise_Exception (Foo_Error'Identity, "This is the exception message");
|
||||
..
|
||||
exception
|
||||
when Error : others =>
|
||||
Put_Line ("Something is wrong here" & Exception_Information (Error));
|
||||
end Main;
|
||||
6
Task/Exceptions/Aikido/exceptions-1.aikido
Normal file
6
Task/Exceptions/Aikido/exceptions-1.aikido
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
try {
|
||||
var lines = readfile ("input.txt")
|
||||
process (lines)
|
||||
} catch (e) {
|
||||
do_somthing(e)
|
||||
}
|
||||
7
Task/Exceptions/Aikido/exceptions-2.aikido
Normal file
7
Task/Exceptions/Aikido/exceptions-2.aikido
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
if (error) {
|
||||
throw "Error"
|
||||
}
|
||||
|
||||
if (something) {
|
||||
throw new MyException (errorcode, a, b)
|
||||
}
|
||||
4
Task/Exceptions/AppleScript/exceptions-1.applescript
Normal file
4
Task/Exceptions/AppleScript/exceptions-1.applescript
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
try
|
||||
set num to 1 / 0
|
||||
--do something that might throw an error
|
||||
end try
|
||||
7
Task/Exceptions/AppleScript/exceptions-2.applescript
Normal file
7
Task/Exceptions/AppleScript/exceptions-2.applescript
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
try
|
||||
set num to 1 / 0
|
||||
--do something that might throw an error
|
||||
on error errMess number errNum
|
||||
--errMess and number errNum are optional
|
||||
display alert "Error # " & errNum & return & errMess
|
||||
end try
|
||||
1
Task/Exceptions/AppleScript/exceptions-3.applescript
Normal file
1
Task/Exceptions/AppleScript/exceptions-3.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
error "Error message." number 2000
|
||||
8
Task/Exceptions/AutoHotkey/exceptions-1.ahk
Normal file
8
Task/Exceptions/AutoHotkey/exceptions-1.ahk
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
try
|
||||
BadlyCodedFunc()
|
||||
catch e
|
||||
MsgBox % "Error in " e.What ", which was called at line " e.Line
|
||||
|
||||
BadlyCodedFunc() {
|
||||
throw Exception("Fail", -1)
|
||||
}
|
||||
12
Task/Exceptions/AutoHotkey/exceptions-2.ahk
Normal file
12
Task/Exceptions/AutoHotkey/exceptions-2.ahk
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
foo()
|
||||
If ErrorLevel
|
||||
Msgbox calling foo failed with: %ErrorLevel%
|
||||
|
||||
foo()
|
||||
{
|
||||
If success
|
||||
Return
|
||||
Else
|
||||
ErrorLevel = foo_error
|
||||
Return
|
||||
}
|
||||
10
Task/Exceptions/BBC-BASIC/exceptions.bbc
Normal file
10
Task/Exceptions/BBC-BASIC/exceptions.bbc
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
ON ERROR PROCerror(ERR, REPORT$) : END
|
||||
|
||||
ERROR 100, "User-generated exception"
|
||||
END
|
||||
|
||||
DEF PROCerror(er%, rpt$)
|
||||
PRINT "Exception occurred"
|
||||
PRINT "Error number was " ; er%
|
||||
PRINT "Error string was " rpt$
|
||||
ENDPROC
|
||||
4
Task/Exceptions/C++/exceptions-1.cpp
Normal file
4
Task/Exceptions/C++/exceptions-1.cpp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
struct MyException
|
||||
{
|
||||
// data with info about exception
|
||||
};
|
||||
5
Task/Exceptions/C++/exceptions-2.cpp
Normal file
5
Task/Exceptions/C++/exceptions-2.cpp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#include <exception>
|
||||
struct MyException: std::exception
|
||||
{
|
||||
char const* what() const throw() { return "description"; }
|
||||
}
|
||||
17
Task/Exceptions/C++/exceptions-3.cpp
Normal file
17
Task/Exceptions/C++/exceptions-3.cpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// this function can throw any type of exception
|
||||
void foo()
|
||||
{
|
||||
throw MyException();
|
||||
}
|
||||
|
||||
// this function can only throw the types of exceptions that are listed
|
||||
void foo2() throw(MyException)
|
||||
{
|
||||
throw MyException();
|
||||
}
|
||||
|
||||
// this function turns any exceptions other than MyException into std::bad_exception
|
||||
void foo3() throw(MyException, std::bad_exception)
|
||||
{
|
||||
throw MyException();
|
||||
}
|
||||
17
Task/Exceptions/C++/exceptions-4.cpp
Normal file
17
Task/Exceptions/C++/exceptions-4.cpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
try {
|
||||
foo();
|
||||
}
|
||||
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
|
||||
}
|
||||
27
Task/Exceptions/C/exceptions-1.c
Normal file
27
Task/Exceptions/C/exceptions-1.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <setjmp.h>
|
||||
|
||||
enum { MY_EXCEPTION = 1 }; /* any non-zero number */
|
||||
|
||||
jmp_buf env;
|
||||
|
||||
void foo()
|
||||
{
|
||||
longjmp(env, MY_EXCEPTION); /* throw MY_EXCEPTION */
|
||||
}
|
||||
|
||||
void call_foo()
|
||||
{
|
||||
switch (setjmp(env)) {
|
||||
case 0: /* try */
|
||||
foo();
|
||||
break;
|
||||
case MY_EXCEPTION: /* catch MY_EXCEPTION */
|
||||
/* handle exceptions of type MY_EXCEPTION */
|
||||
break;
|
||||
default:
|
||||
/* handle any type of exception not handled by above catches */
|
||||
/* note: if this "default" section is not included, that would be equivalent to a blank "default" section */
|
||||
/* i.e. any exception not caught above would be caught and ignored */
|
||||
/* there is no way to "let the exception through" */
|
||||
}
|
||||
}
|
||||
82
Task/Exceptions/C/exceptions-2.c
Normal file
82
Task/Exceptions/C/exceptions-2.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum exceptions {
|
||||
EXCEPTION_1 = 1,
|
||||
EXCEPTION_2,
|
||||
EXCEPTION_3
|
||||
};
|
||||
|
||||
|
||||
#define throw(exception) do { \
|
||||
if (exp) \
|
||||
longjmp(*exp, exception); \
|
||||
printf("uncaught exception %d\n", exception); \
|
||||
exit(exception); \
|
||||
} while (0)
|
||||
|
||||
#define try(block, catch_block) \
|
||||
{ \
|
||||
jmp_buf *exception_outer = exp; \
|
||||
jmp_buf exception_inner; \
|
||||
exp = &exception_inner; \
|
||||
int exception = setjmp(*exp); \
|
||||
if (!exception) { \
|
||||
do block while(0); \
|
||||
exp = exception_outer; \
|
||||
} else { \
|
||||
exp = exception_outer; \
|
||||
switch(exception) { \
|
||||
catch_block \
|
||||
default: \
|
||||
throw(exception); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define catch(exception, block) \
|
||||
case exception: do block while (0); break;
|
||||
|
||||
|
||||
#define throws jmp_buf* exp
|
||||
|
||||
// define a throwing function
|
||||
void g(throws) {
|
||||
printf("g !\n");
|
||||
throw(EXCEPTION_1);
|
||||
printf("shouldnt b here\n");
|
||||
}
|
||||
|
||||
void h(throws, int a)
|
||||
{
|
||||
printf("h %d!\n", a);
|
||||
throw(EXCEPTION_2);
|
||||
}
|
||||
|
||||
void f(throws) {
|
||||
try({
|
||||
g(exp); // call g with intention to catch exceptions
|
||||
},
|
||||
catch(EXCEPTION_1, {
|
||||
printf("exception 1\n");
|
||||
h(exp, 50); // will throw exception 2 inside this catch block
|
||||
}))
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
throws = NULL; // define exception stack base
|
||||
try({
|
||||
f(exp);
|
||||
},
|
||||
catch(EXCEPTION_2, {
|
||||
printf("exception 2\n");
|
||||
})
|
||||
catch(EXCEPTION_3, {
|
||||
printf("exception 3\n");
|
||||
}))
|
||||
|
||||
h(exp, 60); // will result in "uncaught exception"
|
||||
return 0;
|
||||
}
|
||||
8
Task/Exceptions/Clojure/exceptions.clj
Normal file
8
Task/Exceptions/Clojure/exceptions.clj
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(try
|
||||
(if (> (rand) 0.5)
|
||||
(throw (RuntimeException. "oops!"))
|
||||
(println "see this half the time")
|
||||
(catch RuntimeException e
|
||||
(println e)
|
||||
(finally
|
||||
(println "always see this"))
|
||||
5
Task/Exceptions/ColdFusion/exceptions-1.cfm
Normal file
5
Task/Exceptions/ColdFusion/exceptions-1.cfm
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
try {
|
||||
foo();
|
||||
} catch (Any e) {
|
||||
// handle exception e
|
||||
}
|
||||
4
Task/Exceptions/ColdFusion/exceptions-2.cfm
Normal file
4
Task/Exceptions/ColdFusion/exceptions-2.cfm
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<cftry>
|
||||
<cfcatch type="Database|...">
|
||||
</cfcatch>
|
||||
</cftry>
|
||||
14
Task/Exceptions/Common-Lisp/exceptions.lisp
Normal file
14
Task/Exceptions/Common-Lisp/exceptions.lisp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(define-condition unexpected-odd-number (error)
|
||||
((number :reader number :initarg :number))
|
||||
(:report (lambda (condition stream)
|
||||
(format stream "Unexpected odd number: ~w."
|
||||
(number condition)))))
|
||||
|
||||
(defun get-number (&aux (n (random 100)))
|
||||
(if (not (oddp n)) n
|
||||
(error 'unexpected-odd-number :number n)))
|
||||
|
||||
(defun get-even-number ()
|
||||
(handler-case (get-number)
|
||||
(unexpected-odd-number (condition)
|
||||
(1+ (number condition)))))
|
||||
35
Task/Exceptions/D/exceptions.d
Normal file
35
Task/Exceptions/D/exceptions.d
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import std.stdio;
|
||||
|
||||
/// Throw Exceptions
|
||||
/// Stack traces are generated compiling with the -g switch.
|
||||
void test1() {
|
||||
throw new Exception("Sample Exception");
|
||||
}
|
||||
|
||||
/// Catch Exceptions
|
||||
void test2() {
|
||||
try {
|
||||
test1();
|
||||
} catch (Exception ex) {
|
||||
writeln(ex);
|
||||
throw ex; // rethrow
|
||||
}
|
||||
}
|
||||
|
||||
/// Ways to implement finally
|
||||
void test3() {
|
||||
try test2();
|
||||
finally writeln("test3 finally");
|
||||
}
|
||||
|
||||
/// Or also with scope guards
|
||||
void test4() {
|
||||
scope(exit) writeln("Test4 done");
|
||||
scope(failure) writeln("Test4 exited by exception");
|
||||
scope(success) writeln("Test4 exited by return or function end");
|
||||
test2();
|
||||
}
|
||||
|
||||
void main() {
|
||||
test4();
|
||||
}
|
||||
4
Task/Exceptions/DWScript/exceptions-1.dwscript
Normal file
4
Task/Exceptions/DWScript/exceptions-1.dwscript
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
procedure Test;
|
||||
begin
|
||||
raise Exception.Create('Sample Exception');
|
||||
end;
|
||||
11
Task/Exceptions/DWScript/exceptions-2.dwscript
Normal file
11
Task/Exceptions/DWScript/exceptions-2.dwscript
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
procedure Test2;
|
||||
begin
|
||||
try
|
||||
test;
|
||||
except
|
||||
on E: Exception do begin // Filter by exception class
|
||||
PrintLn(E.Message); // Showing exception message
|
||||
raise; // Rethrowing
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
8
Task/Exceptions/DWScript/exceptions-3.dwscript
Normal file
8
Task/Exceptions/DWScript/exceptions-3.dwscript
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
procedure Test3;
|
||||
begin
|
||||
try
|
||||
test2;
|
||||
finally
|
||||
PrintLn('Test3 finally');
|
||||
end;
|
||||
end;
|
||||
4
Task/Exceptions/Delphi/exceptions-1.delphi
Normal file
4
Task/Exceptions/Delphi/exceptions-1.delphi
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
procedure test;
|
||||
begin
|
||||
raise Exception.Create('Sample Exception');
|
||||
end;
|
||||
9
Task/Exceptions/Delphi/exceptions-2.delphi
Normal file
9
Task/Exceptions/Delphi/exceptions-2.delphi
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure test2;
|
||||
begin
|
||||
try
|
||||
test;
|
||||
except
|
||||
ShowMessage(Exception(ExceptObject).Message); // Showing exception message
|
||||
raise; // Rethrowing
|
||||
end;
|
||||
end;
|
||||
8
Task/Exceptions/Delphi/exceptions-3.delphi
Normal file
8
Task/Exceptions/Delphi/exceptions-3.delphi
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
procedure test3;
|
||||
begin
|
||||
try
|
||||
test2;
|
||||
finally
|
||||
ShowMessage('test3 finally');
|
||||
end;
|
||||
end;
|
||||
15
Task/Exceptions/E/exceptions-1.e
Normal file
15
Task/Exceptions/E/exceptions-1.e
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
def nameOf(arg :int) {
|
||||
if (arg == 43) {
|
||||
return "Bob"
|
||||
} else {
|
||||
throw("Who?")
|
||||
}
|
||||
}
|
||||
|
||||
def catching(arg) {
|
||||
try {
|
||||
return ["ok", nameOf(arg)]
|
||||
} catch exceptionObj {
|
||||
return ["notok", exceptionObj]
|
||||
}
|
||||
}
|
||||
8
Task/Exceptions/E/exceptions-2.e
Normal file
8
Task/Exceptions/E/exceptions-2.e
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
? catching(42)
|
||||
# value: ["not ok", problem: Who?]
|
||||
|
||||
? catching(43)
|
||||
# value: ["ok", "Bob"]
|
||||
|
||||
? catching(45.7)
|
||||
# value: ["not ok", problem: the float64 45.7 doesn't coerce to an int]
|
||||
15
Task/Exceptions/E/exceptions-3.e
Normal file
15
Task/Exceptions/E/exceptions-3.e
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
def nameOf(arg :int, ejector) {
|
||||
if (arg == 43) {
|
||||
return "Bob"
|
||||
} else {
|
||||
ejector("Who?")
|
||||
}
|
||||
}
|
||||
|
||||
def catching(arg) {
|
||||
escape unnamed {
|
||||
return ["ok", nameOf(arg, unnamed)]
|
||||
} catch exceptionObj {
|
||||
return ["notok", exceptionObj]
|
||||
}
|
||||
}
|
||||
8
Task/Exceptions/E/exceptions-4.e
Normal file
8
Task/Exceptions/E/exceptions-4.e
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
? catching(42)
|
||||
# value: ["not ok", problem: Who?]
|
||||
|
||||
? catching(43)
|
||||
# value: ["ok", "Bob"]
|
||||
|
||||
? catching(45.7)
|
||||
# problem: the float64 45.7 doesn't coerce to an int
|
||||
11
Task/Exceptions/E/exceptions-5.e
Normal file
11
Task/Exceptions/E/exceptions-5.e
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var nameTable := null
|
||||
def nameOf(arg :int, ejector) {
|
||||
if (nameTable == null) {
|
||||
nameTable := <import:nameTableParser>.parseFile(<file:nameTable.txt>)
|
||||
}
|
||||
if (nameTable.maps(arg)) {
|
||||
return nameTable[arg]
|
||||
} else {
|
||||
ejector(makeNotFoundException("Who?"))
|
||||
}
|
||||
}
|
||||
2
Task/Exceptions/Forth/exceptions-1.fth
Normal file
2
Task/Exceptions/Forth/exceptions-1.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: f ( -- ) 1 throw ." f " ; \ will throw a "1"
|
||||
: g ( -- ) 0 throw ." g " ; \ does not throw
|
||||
4
Task/Exceptions/Forth/exceptions-2.fth
Normal file
4
Task/Exceptions/Forth/exceptions-2.fth
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
: report ( n -- ) ?dup if ." caught " . else ." no throw" then ;
|
||||
: test ( -- )
|
||||
['] f catch report
|
||||
['] g catch report ;
|
||||
2
Task/Exceptions/Forth/exceptions-3.fth
Normal file
2
Task/Exceptions/Forth/exceptions-3.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
cr test
|
||||
'''caught 1 g no throw ok'''
|
||||
1
Task/Exceptions/Forth/exceptions-4.fth
Normal file
1
Task/Exceptions/Forth/exceptions-4.fth
Normal file
|
|
@ -0,0 +1 @@
|
|||
10 ['] myfun catch if drop then
|
||||
19
Task/Exceptions/Go/exceptions.go
Normal file
19
Task/Exceptions/Go/exceptions.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func foo() {
|
||||
fmt.Println("let's foo...")
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
}
|
||||
}()
|
||||
panic("FAIL!")
|
||||
fmt.Println("there's no point in going on.")
|
||||
}
|
||||
|
||||
func main() {
|
||||
foo()
|
||||
fmt.Println("glad that's over.")
|
||||
}
|
||||
2
Task/Exceptions/Haskell/exceptions-1.hs
Normal file
2
Task/Exceptions/Haskell/exceptions-1.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
do {- ... -}
|
||||
throwIO SomeException
|
||||
2
Task/Exceptions/Haskell/exceptions-2.hs
Normal file
2
Task/Exceptions/Haskell/exceptions-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
if condition then 3
|
||||
else throw SomeException
|
||||
2
Task/Exceptions/Haskell/exceptions-3.hs
Normal file
2
Task/Exceptions/Haskell/exceptions-3.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
if condition then 3
|
||||
else throwDyn myException
|
||||
4
Task/Exceptions/Haskell/exceptions-4.hs
Normal file
4
Task/Exceptions/Haskell/exceptions-4.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
do
|
||||
{- do IO computations here -}
|
||||
`catch` \ex -> do
|
||||
{- handle exception "ex" here -}
|
||||
4
Task/Exceptions/Haskell/exceptions-5.hs
Normal file
4
Task/Exceptions/Haskell/exceptions-5.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
do
|
||||
{- do IO computations here -}
|
||||
`catchDyn` \ex -> do
|
||||
{- handle exception "ex" here -}
|
||||
17
Task/Exceptions/Haskell/exceptions-6.hs
Normal file
17
Task/Exceptions/Haskell/exceptions-6.hs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import Exceptions
|
||||
|
||||
procedure main(A)
|
||||
every i := !A do {
|
||||
case Try().call{ write(g(i)) } of {
|
||||
Try().catch(): {
|
||||
x := Try().getException()
|
||||
write(x.getMessage(), ":\n", x.getLocation())
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
procedure g(i)
|
||||
if numeric(i) = 3 then Exception().throw("bad value of "||i)
|
||||
return i
|
||||
end
|
||||
7
Task/Exceptions/Java/exceptions-1.java
Normal file
7
Task/Exceptions/Java/exceptions-1.java
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
//Checked exception
|
||||
public class MyException extends Exception {
|
||||
//Put specific info in here
|
||||
}
|
||||
|
||||
//Unchecked exception
|
||||
public class MyRuntimeException extends RuntimeException {}
|
||||
7
Task/Exceptions/Java/exceptions-2.java
Normal file
7
Task/Exceptions/Java/exceptions-2.java
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
public void fooChecked() throws MyException {
|
||||
throw new MyException();
|
||||
}
|
||||
|
||||
public void fooUnchecked() {
|
||||
throw new MyRuntimeException();
|
||||
}
|
||||
15
Task/Exceptions/Java/exceptions-3.java
Normal file
15
Task/Exceptions/Java/exceptions-3.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
try {
|
||||
fooChecked();
|
||||
}
|
||||
catch(MyException exc) {
|
||||
//Catch only your specified type of exception
|
||||
}
|
||||
catch(Exception exc) {
|
||||
//Catch any non-system error exception
|
||||
}
|
||||
catch(Throwable exc) {
|
||||
//Catch everything including system errors (not recommended)
|
||||
}
|
||||
finally {
|
||||
//This code is always executed after exiting the try block
|
||||
}
|
||||
13
Task/Exceptions/Java/exceptions-4.java
Normal file
13
Task/Exceptions/Java/exceptions-4.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
public void foo() throws UnsupportedDataTypeException{
|
||||
try{
|
||||
throwsNumberFormatException();
|
||||
//the following methods throw exceptions which extend IOException
|
||||
throwsUnsupportedDataTypeException();
|
||||
throwsFileNotFoundException();
|
||||
}catch(FileNotFoundException | NumberFormatException ex){
|
||||
//deal with these two Exceptions without duplicating code
|
||||
}catch(IOException e){
|
||||
//deal with the UnsupportedDataTypeException as well as any other unchecked IOExceptions
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
3
Task/Exceptions/JavaScript/exceptions-1.js
Normal file
3
Task/Exceptions/JavaScript/exceptions-1.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
function doStuff() {
|
||||
throw new Error('Not implemented!');
|
||||
}
|
||||
9
Task/Exceptions/JavaScript/exceptions-2.js
Normal file
9
Task/Exceptions/JavaScript/exceptions-2.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
try {
|
||||
element.attachEvent('onclick', doStuff);
|
||||
}
|
||||
catch(e if e instanceof TypeError) {
|
||||
element.addEventListener('click', doStuff, false);
|
||||
}
|
||||
finally {
|
||||
eventSetup = true;
|
||||
}
|
||||
1
Task/Exceptions/Lua/exceptions-1.lua
Normal file
1
Task/Exceptions/Lua/exceptions-1.lua
Normal file
|
|
@ -0,0 +1 @@
|
|||
error("Something bad happened!")
|
||||
11
Task/Exceptions/Lua/exceptions-2.lua
Normal file
11
Task/Exceptions/Lua/exceptions-2.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function throw_error()
|
||||
error("Whoops")
|
||||
-- won't ever appear, due to previous error() call
|
||||
return "hello!"
|
||||
end
|
||||
|
||||
-- 'status' is false if 'throw_error' threw an error
|
||||
-- otherwise, when everything went well, it will be true.
|
||||
-- 'errmsg' contains the error message, plus filename and line number of where the error occured
|
||||
status, errmsg = pcall(throw_error)
|
||||
print("errmsg = ", errmsg)
|
||||
8
Task/Exceptions/Lua/exceptions-3.lua
Normal file
8
Task/Exceptions/Lua/exceptions-3.lua
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function throw_error_with_argment(argument)
|
||||
error(string.format("Whoops! argument = %s", argument))
|
||||
-- won't ever appear, due to previous error() call
|
||||
return "hello!"
|
||||
end
|
||||
|
||||
status, errmsg = pcall(throw_error_with_argment, "foobar 123")
|
||||
print("errmsg = ", errmsg)
|
||||
6
Task/Exceptions/Lua/exceptions-4.lua
Normal file
6
Task/Exceptions/Lua/exceptions-4.lua
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
function throw_error_with_argment(argument)
|
||||
return "hello!"
|
||||
end
|
||||
|
||||
status, errmsg = pcall(throw_error_with_argment, "foobar 123")
|
||||
print("errmsg = ", errmsg)
|
||||
2
Task/Exceptions/MATLAB/exceptions.m
Normal file
2
Task/Exceptions/MATLAB/exceptions.m
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
>> error 'Help'
|
||||
??? Help
|
||||
4
Task/Exceptions/PHP/exceptions-1.php
Normal file
4
Task/Exceptions/PHP/exceptions-1.php
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
class MyException extends Exception
|
||||
{
|
||||
// Custom exception attributes & methods
|
||||
}
|
||||
4
Task/Exceptions/PHP/exceptions-2.php
Normal file
4
Task/Exceptions/PHP/exceptions-2.php
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
function throwsException()
|
||||
{
|
||||
throw new Exception('Exception message');
|
||||
}
|
||||
5
Task/Exceptions/PHP/exceptions-3.php
Normal file
5
Task/Exceptions/PHP/exceptions-3.php
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
try {
|
||||
throwsException();
|
||||
} catch (Exception $e) {
|
||||
echo 'Caught exception: ' . $e->getMessage();
|
||||
}
|
||||
11
Task/Exceptions/Perl/exceptions-1.pl
Normal file
11
Task/Exceptions/Perl/exceptions-1.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# throw an exception
|
||||
die "Danger, danger, Will Robinson!";
|
||||
|
||||
# catch an exception and show it
|
||||
eval {
|
||||
die "this could go wrong mightily";
|
||||
};
|
||||
print $@ if $@;
|
||||
|
||||
# rethrow
|
||||
die $@;
|
||||
2
Task/Exceptions/Perl/exceptions-2.pl
Normal file
2
Task/Exceptions/Perl/exceptions-2.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# throw an exception
|
||||
die "Danger, danger, Will Robinson!";
|
||||
6
Task/Exceptions/Perl/exceptions-3.pl
Normal file
6
Task/Exceptions/Perl/exceptions-3.pl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# catch an exception and show it
|
||||
try {
|
||||
die "this could go wrong mightily";
|
||||
} catch {
|
||||
print;
|
||||
};
|
||||
2
Task/Exceptions/Perl/exceptions-4.pl
Normal file
2
Task/Exceptions/Perl/exceptions-4.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# rethrow (inside of catch)
|
||||
die $_;
|
||||
4
Task/Exceptions/PicoLisp/exceptions.l
Normal file
4
Task/Exceptions/PicoLisp/exceptions.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(catch 'thisLabel # Catch this label
|
||||
(println 1) # Do some processing (print '1')
|
||||
(throw 'thisLabel 2) # Abort processing and return '2'
|
||||
(println 3) ) # This is never reached
|
||||
4
Task/Exceptions/Python/exceptions-1.py
Normal file
4
Task/Exceptions/Python/exceptions-1.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import exceptions
|
||||
class SillyError(exceptions.Exception):
|
||||
def __init__(self,args=None):
|
||||
self.args=args
|
||||
2
Task/Exceptions/Python/exceptions-2.py
Normal file
2
Task/Exceptions/Python/exceptions-2.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
class MyInvalidArgument(ValueError):
|
||||
pass
|
||||
2
Task/Exceptions/Python/exceptions-3.py
Normal file
2
Task/Exceptions/Python/exceptions-3.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def spam():
|
||||
raise SillyError # equivalent to raise SillyError()
|
||||
2
Task/Exceptions/Python/exceptions-4.py
Normal file
2
Task/Exceptions/Python/exceptions-4.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def spam():
|
||||
raise SillyError, 'egg' # equivalent to raise SillyError('egg')
|
||||
2
Task/Exceptions/Python/exceptions-5.py
Normal file
2
Task/Exceptions/Python/exceptions-5.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def spam():
|
||||
raise SillyError('egg')
|
||||
10
Task/Exceptions/Python/exceptions-6.py
Normal file
10
Task/Exceptions/Python/exceptions-6.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
try:
|
||||
foo()
|
||||
except SillyError, se:
|
||||
print se.args
|
||||
bar()
|
||||
else:
|
||||
# no exception occurred
|
||||
quux()
|
||||
finally:
|
||||
baz()
|
||||
10
Task/Exceptions/Python/exceptions-7.py
Normal file
10
Task/Exceptions/Python/exceptions-7.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
try:
|
||||
foo()
|
||||
except SillyError as se:
|
||||
print(se.args)
|
||||
bar()
|
||||
else:
|
||||
# no exception occurred
|
||||
quux()
|
||||
finally:
|
||||
baz()
|
||||
1
Task/Exceptions/R/exceptions-1.r
Normal file
1
Task/Exceptions/R/exceptions-1.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
e <- simpleError("This is a simpleError")
|
||||
2
Task/Exceptions/R/exceptions-2.r
Normal file
2
Task/Exceptions/R/exceptions-2.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
stop("An error has occured")
|
||||
stop(e) #where e is a simpleError, as above
|
||||
13
Task/Exceptions/R/exceptions-3.r
Normal file
13
Task/Exceptions/R/exceptions-3.r
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
tryCatch(
|
||||
{
|
||||
if(runif(1) > 0.5)
|
||||
{
|
||||
message("This doesn't throw an error")
|
||||
} else
|
||||
{
|
||||
stop("This is an error")
|
||||
}
|
||||
},
|
||||
error = function(e) message(paste("An error occured", e$message, sep = ": ")),
|
||||
finally = message("This is called whether or not an exception occured")
|
||||
)
|
||||
13
Task/Exceptions/Racket/exceptions.rkt
Normal file
13
Task/Exceptions/Racket/exceptions.rkt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#lang racket
|
||||
|
||||
;; define a new exception type
|
||||
(struct exn:my-exception exn ())
|
||||
|
||||
;; handler that prints the message ("Hi!")
|
||||
(define (handler exn)
|
||||
(displayln (exn-message exn)))
|
||||
|
||||
;; install exception handlers
|
||||
(with-handlers ([exn:my-exception? handler])
|
||||
;; raise the exception
|
||||
(raise (exn:my-exception "Hi!" (current-continuation-marks))))
|
||||
3
Task/Exceptions/Ruby/exceptions-1.rb
Normal file
3
Task/Exceptions/Ruby/exceptions-1.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# define an exception
|
||||
class SillyError < Exception
|
||||
end
|
||||
2
Task/Exceptions/Ruby/exceptions-2.rb
Normal file
2
Task/Exceptions/Ruby/exceptions-2.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
class MyInvalidArgument < ArgumentError
|
||||
end
|
||||
11
Task/Exceptions/Ruby/exceptions-3.rb
Normal file
11
Task/Exceptions/Ruby/exceptions-3.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# raise (throw) an exception
|
||||
def spam
|
||||
raise SillyError, 'egg'
|
||||
end
|
||||
|
||||
# rescue (catch) an exception
|
||||
begin
|
||||
spam
|
||||
rescue SillyError => se
|
||||
puts se # writes 'egg' to stdout
|
||||
end
|
||||
15
Task/Exceptions/Ruby/exceptions-4.rb
Normal file
15
Task/Exceptions/Ruby/exceptions-4.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
begin
|
||||
foo
|
||||
rescue ArgumentError => e
|
||||
# rescues a MyInvalidArgument or any other ArgumentError
|
||||
bar
|
||||
rescue => e
|
||||
# rescues a StandardError
|
||||
quack
|
||||
else
|
||||
# runs if no exception occurred
|
||||
quux
|
||||
ensure
|
||||
# always runs
|
||||
baz
|
||||
end
|
||||
2
Task/Exceptions/Ruby/exceptions-5.rb
Normal file
2
Task/Exceptions/Ruby/exceptions-5.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# short way to rescue any StandardError
|
||||
quotient = 1 / 0 rescue "sorry"
|
||||
7
Task/Exceptions/Ruby/exceptions-6.rb
Normal file
7
Task/Exceptions/Ruby/exceptions-6.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def foo
|
||||
throw :done
|
||||
end
|
||||
|
||||
catch :done do
|
||||
foo
|
||||
end
|
||||
22
Task/Exceptions/Scheme/exceptions.ss
Normal file
22
Task/Exceptions/Scheme/exceptions.ss
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(define (me-errors xx exception)
|
||||
(if (even? xx)
|
||||
xx
|
||||
(exception)))
|
||||
|
||||
;example that does nothing special on exception
|
||||
(call/cc
|
||||
(lambda (exception)
|
||||
(me-errors 222 exception)
|
||||
(display "I guess everything is alright")))
|
||||
|
||||
;example that laments oddness on exception
|
||||
(call/cc
|
||||
(lambda (all-ok) ;used to "jump" over exception handling
|
||||
|
||||
(call/cc
|
||||
(lambda (exception-handle)
|
||||
(me-errors 333 exception-handle)
|
||||
(display "I guess everything is alright")
|
||||
(all-ok)))
|
||||
|
||||
(display "oh my god it is ODD!")))
|
||||
5
Task/Exceptions/Smalltalk/exceptions-1.st
Normal file
5
Task/Exceptions/Smalltalk/exceptions-1.st
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
"exec" "gst" "-f" "$0" "$0" "$*"
|
||||
"exit"
|
||||
|
||||
Transcript show: 'Throwing yawp'; cr.
|
||||
self error: 'Yawp!'.
|
||||
7
Task/Exceptions/Smalltalk/exceptions-2.st
Normal file
7
Task/Exceptions/Smalltalk/exceptions-2.st
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
$ ./yawp.st
|
||||
Throwing yawp
|
||||
Object: nil error: Yawp!
|
||||
Error(Exception)>>signal (AnsiExcept.st:216)
|
||||
Error(Exception)>>signal: (AnsiExcept.st:226)
|
||||
UndefinedObject(Object)>>error: (AnsiExcept.st:1565)
|
||||
UndefinedObject>>executeStatements (yawp.st:5)
|
||||
9
Task/Exceptions/Smalltalk/exceptions-3.st
Normal file
9
Task/Exceptions/Smalltalk/exceptions-3.st
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
"exec" "gst" "-f" "$0" "$0" "$*"
|
||||
"exit"
|
||||
|
||||
[
|
||||
Transcript show: 'Throwing yawp'; cr.
|
||||
self error: 'Yawp!'.
|
||||
] on: Error do: [ :e |
|
||||
Transcript show: 'Caught yawp'; cr.
|
||||
].
|
||||
3
Task/Exceptions/Smalltalk/exceptions-4.st
Normal file
3
Task/Exceptions/Smalltalk/exceptions-4.st
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$ ./yawp.st
|
||||
Throwing yawp
|
||||
Caught yawp
|
||||
15
Task/Exceptions/Tcl/exceptions.tcl
Normal file
15
Task/Exceptions/Tcl/exceptions.tcl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package require Tcl 8.5
|
||||
|
||||
# Throw
|
||||
proc e {args} {
|
||||
error "error message" "error message for stack trace" {errorCode list}
|
||||
}
|
||||
|
||||
# Catch and rethrow
|
||||
proc f {} {
|
||||
if {[catch {e 1 2 3 4} errMsg options] != 0} {
|
||||
return -options $options $errMsg
|
||||
}
|
||||
}
|
||||
|
||||
f
|
||||
Loading…
Add table
Add a link
Reference in a new issue