Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Exceptions/00-META.yaml
Normal file
3
Task/Exceptions/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Exceptions
|
||||
note: Control Structures
|
||||
13
Task/Exceptions/00-TASK.txt
Normal file
13
Task/Exceptions/00-TASK.txt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
{{Control Structures}}
|
||||
{{omit from|Euphoria}}
|
||||
{{omit from|M4}}
|
||||
{{omit from|Retro}}
|
||||
|
||||
This task is to give an example of an exception handling routine
|
||||
and to "throw" a new exception.
|
||||
|
||||
|
||||
;Related task:
|
||||
* [[Exceptions Through Nested Calls]]
|
||||
<br><br>
|
||||
|
||||
9
Task/Exceptions/11l/exceptions.11l
Normal file
9
Task/Exceptions/11l/exceptions.11l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
T SillyError
|
||||
String message
|
||||
F (message)
|
||||
.message = message
|
||||
|
||||
X.try
|
||||
X SillyError(‘egg’)
|
||||
X.catch SillyError se
|
||||
print(se.message)
|
||||
26
Task/Exceptions/8086-Assembly/exceptions.8086
Normal file
26
Task/Exceptions/8086-Assembly/exceptions.8086
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
;syscall for creating a new file.
|
||||
mov dx,offset filename
|
||||
mov cx,0
|
||||
mov ah,5Bh
|
||||
int 21h
|
||||
;if error occurs, will return carry set and error code in ax
|
||||
;Error code 03h = path not found
|
||||
;Error code 04h = Too many open files
|
||||
;Error code 05h = Access denied
|
||||
;Error code 50h = File already exists
|
||||
|
||||
jnc noError ;continue with program
|
||||
|
||||
cmp ax,03h
|
||||
je PathNotFoundError ;unimplemented exception handler
|
||||
|
||||
cmp ax,04h
|
||||
je TooManyOpenFilesError
|
||||
|
||||
cmp ax,05h
|
||||
je AccessDeniedError
|
||||
|
||||
cmp ax,50h
|
||||
je FileAlreadyExistsError
|
||||
|
||||
noError:
|
||||
119
Task/Exceptions/ALGOL-68/exceptions-1.alg
Normal file
119
Task/Exceptions/ALGOL-68/exceptions-1.alg
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
COMMENT
|
||||
Define an general event handling mechanism on MODE OBJ:
|
||||
* try to parallel pythons exception handling flexibility
|
||||
END COMMENT
|
||||
|
||||
COMMENT
|
||||
REQUIRES:
|
||||
MODE OBJ # These can be a UNION of REF types #
|
||||
OP OBJIS
|
||||
PROVIDES:
|
||||
OP ON, RAISE, RESET
|
||||
PROC obj on, obj raise, obj reset
|
||||
END COMMENT
|
||||
|
||||
# define object related to OBJ EVENTS #
|
||||
MODE
|
||||
RAISEOBJ = PROC(OBJ)VOID, RAWMENDOBJ = PROC(OBJ)BOOL,
|
||||
MENDOBJ = UNION(RAWMENDOBJ, PROC VOID), # Generalise: Allow PROC VOID (a GOTO) as a short hand #
|
||||
NEWSCOPEOBJ = STRUCT(REF NEWSCOPEOBJ up, FLEXOBJ obj flex, FLEXEVENTOBJ event flex, MENDOBJ mended),
|
||||
SCOPEOBJ = REF NEWSCOPEOBJ;
|
||||
|
||||
MODE FLEXOBJ=FLEX[0]OBJ;
|
||||
|
||||
# Provide an INIT to convert a GO TO to a MEND ... useful for direct aborts #
|
||||
OP INITMENDOBJ = (PROC VOID go to)MENDOBJ: (go to; SKIP);
|
||||
|
||||
SCOPEOBJ obj scope end = NIL;
|
||||
SCOPEOBJ obj scope begin := obj scope end; # INITialise stack #
|
||||
OBJ obj any = EMPTY;
|
||||
EVENTOBJ obj event any = NIL;
|
||||
|
||||
# Some crude Singly Linked-List manipulations of the scopes, aka stack ... #
|
||||
# An event/mended can be shared for all OBJ of the same type: #
|
||||
PRIO INITAB = 1, +=: = 1;
|
||||
OP INITAB = (SCOPEOBJ lhs, MENDOBJ obj mend)SCOPEOBJ:
|
||||
lhs := (obj scope end, obj any, obj event any, obj mend);
|
||||
|
||||
OP INITSCOPE = (MENDOBJ obj mend)SCOPEOBJ: HEAP NEWSCOPEOBJ INITAB obj mend;
|
||||
OP +=: = (SCOPEOBJ item, REF SCOPEOBJ rhs)SCOPEOBJ: ( up OF item := rhs; rhs := item );
|
||||
OP +=: = (MENDOBJ mend, REF SCOPEOBJ rhs)SCOPEOBJ: INITSCOPE mend +=: rhs;
|
||||
#OP -=: = (REF SCOPEOBJ scope)SCOPEOBJ: scope := up OF scope;#
|
||||
|
||||
COMMENT Restore the prio event scope: ~ END COMMENT
|
||||
PROC obj reset = (SCOPEOBJ up scope)VOID: obj scope begin := up scope;
|
||||
MENDOBJ obj unmendable = (OBJ obj)BOOL: FALSE;
|
||||
|
||||
MODE NEWEVENTOBJ = STRUCT( # the is simple a typed place holder #
|
||||
SCOPEOBJ scope,
|
||||
STRING description,
|
||||
PROC (OBJ #obj#, MENDOBJ #obj mend#)SCOPEOBJ on,
|
||||
PROC (OBJ #obj#, STRING #msg#)VOID raise
|
||||
), EVENTOBJ = REF NEWEVENTOBJ;
|
||||
|
||||
MODE FLEXEVENTOBJ = FLEX[0]EVENTOBJ;
|
||||
|
||||
COMMENT Define how to catch an event:
|
||||
obj - IF obj IS NIL then mend event on all OBJects
|
||||
obj mend - PROC to call to repair the object
|
||||
return the prior event scope
|
||||
END COMMENT
|
||||
PROC obj on = (FLEXOBJ obj flex, FLEXEVENTOBJ event flex, MENDOBJ mend)SCOPEOBJ: (
|
||||
mend +=: obj scope begin;
|
||||
IF obj any ISNTIN obj flex THEN obj flex OF obj scope begin := obj flex FI;
|
||||
IF obj event any ISNTIN event flex THEN event flex OF obj scope begin := event flex FI;
|
||||
up OF obj scope begin
|
||||
);
|
||||
|
||||
PRIO OBJIS = 4, OBJISNT = 4; # pick the same PRIOrity as EQ and NE #
|
||||
OP OBJISNT = (OBJ a,b)BOOL: NOT(a OBJIS b);
|
||||
|
||||
PRIO ISIN = 4, ISNTIN = 4;
|
||||
OP ISNTIN = (OBJ obj, FLEXOBJ obj flex)BOOL: (
|
||||
BOOL isnt in := FALSE;
|
||||
FOR i TO UPB obj flex WHILE isnt in := obj OBJISNT obj flex[i] DO SKIP OD;
|
||||
isnt in
|
||||
);
|
||||
OP ISIN = (OBJ obj, FLEXOBJ obj flex)BOOL: NOT(obj ISNTIN obj flex);
|
||||
|
||||
OP ISNTIN = (EVENTOBJ event, FLEXEVENTOBJ event flex)BOOL: (
|
||||
BOOL isnt in := TRUE;
|
||||
FOR i TO UPB event flex WHILE isnt in := event ISNT event flex[i] DO SKIP OD;
|
||||
isnt in
|
||||
);
|
||||
OP ISIN = (EVENTOBJ event, FLEXEVENTOBJ event flex)BOOL: NOT(event ISNTIN event flex);
|
||||
|
||||
COMMENT Define how to raise an event, once it is raised try and mend it:
|
||||
if all else fails produce an error message and stop
|
||||
END COMMENT
|
||||
PROC obj raise = (OBJ obj, EVENTOBJ event, STRING msg)VOID:(
|
||||
SCOPEOBJ this scope := obj scope begin;
|
||||
# until mended this event should cascade through scope event handlers/members #
|
||||
FOR i WHILE this scope ISNT SCOPEOBJ(obj scope end) DO
|
||||
IF (obj any ISIN obj flex OF this scope OR obj ISIN obj flex OF this scope ) AND
|
||||
(obj event any ISIN event flex OF this scope OR event ISIN event flex OF this scope)
|
||||
THEN
|
||||
CASE mended OF this scope IN
|
||||
(RAWMENDOBJ mend):IF mend(obj) THEN break mended FI,
|
||||
(PROC VOID go to): (go to; stop)
|
||||
OUT put(stand error, "undefined: raise stop"); stop
|
||||
ESAC
|
||||
FI;
|
||||
this scope := up OF this scope
|
||||
OD;
|
||||
put(stand error, ("OBJ event: ",msg)); stop; FALSE
|
||||
EXIT
|
||||
break mended: TRUE
|
||||
);
|
||||
|
||||
CO define ON and some useful(?) RAISE OPs CO
|
||||
PRIO ON = 1, RAISE = 1;
|
||||
OP ON = (MENDOBJ mend, EVENTOBJ event)SCOPEOBJ: obj on(obj any, event, mend),
|
||||
RAISE = (OBJ obj, EVENTOBJ event)VOID: obj raise(obj, event, "unnamed event"),
|
||||
RAISE = (OBJ obj, MENDOBJ mend)VOID: ( mend ON obj event any; obj RAISE obj event any),
|
||||
RAISE = (EVENTOBJ event)VOID: obj raise(obj any, event, "unnamed event"),
|
||||
RAISE = (MENDOBJ mend)VOID: ( mend ON obj event any; RAISE obj event any),
|
||||
RAISE = (STRING msg, EVENTOBJ event)VOID: obj raise(obj any, event, msg);
|
||||
OP (SCOPEOBJ #up scope#)VOID RESET = obj reset;
|
||||
|
||||
SKIP
|
||||
48
Task/Exceptions/ALGOL-68/exceptions-2.alg
Normal file
48
Task/Exceptions/ALGOL-68/exceptions-2.alg
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/a68g --script #
|
||||
|
||||
MODE OBJ=UNION(REF INT, REF REAL, REF STRING,# etc # VOID);
|
||||
|
||||
OP OBJIS = (OBJ a,b)BOOL: # Are a and b at the same address? #
|
||||
CASE a IN # Ironically Algol68's STRONG typing means we cannot simply compare addresses #
|
||||
(REF INT a): a IS (b|(REF INT b):b|NIL),
|
||||
(REF REAL a): a IS (b|(REF REAL b):b|NIL),
|
||||
(REF STRING a): a IS (b|(REF STRING b):b|NIL)
|
||||
OUT FALSE
|
||||
ESAC;
|
||||
|
||||
PR READ "prelude/event_base(obj).a68" PR;
|
||||
NEWEVENTOBJ obj eventa := SKIP;
|
||||
NEWEVENTOBJ obj eventb := SKIP;
|
||||
NEWEVENTOBJ user defined exception := SKIP;
|
||||
|
||||
# An event can be continued "mended" or break "unmended" #
|
||||
PROC found sum sqs continue = (OBJ obj)BOOL: ( print("."); TRUE); # mended #
|
||||
PROC found sum sqs break = (OBJ obj)BOOL: (found sq sum sqs; FALSE); # unmended #
|
||||
|
||||
INT sum sqs:=0;
|
||||
REAL x:=111, y:=222, z:=333;
|
||||
|
||||
SCOPEOBJ obj scope reset := obj on((sum sqs, x,y,z), (obj eventa,obj eventb), VOID:found sq sum sqs);
|
||||
|
||||
# An event handler specific to the specific object instance: #
|
||||
#SCOPEOBJ obj scope reset := obj on eventb(sum sqs, VOID:found sq sum sqs);#
|
||||
|
||||
# Or... An "obj any" event handler: #
|
||||
# SCOPEOBJ obj scope reset := found sum sqs break ON obj eventb; #
|
||||
|
||||
# Raise the "event eventb" on an object: #
|
||||
FOR i DO
|
||||
sum sqs +:= i*i;
|
||||
IF sum sqs = 70*70 THEN # 1st try to use an instance specific mend on the object #
|
||||
obj raise(sum sqs, obj eventb, "Found a sq sum of sqs") FI; # OR ... #
|
||||
IF sum sqs = 70*70 THEN "Found a sq sum of sqs" RAISE obj eventb FI; # OR ... #
|
||||
IF sum sqs = 70*70 THEN RAISE found sum sqs break FI # simplest #
|
||||
OD;
|
||||
RESET obj scope reset # need to manually reset back to prior handlers #
|
||||
|
||||
# Catch "event eventb": #
|
||||
EXIT found sq sum sqs:
|
||||
print(("sum sqs:",sum sqs, new line)); # event eventb caught code here ... #
|
||||
RESET obj scope reset;
|
||||
|
||||
"finally: raise the base unmendable event" RAISE obj eventb
|
||||
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)
|
||||
}
|
||||
25
Task/Exceptions/Aime/exceptions-1.aime
Normal file
25
Task/Exceptions/Aime/exceptions-1.aime
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
void
|
||||
throwing(void)
|
||||
{
|
||||
o_text("throwing...\n");
|
||||
error("now!");
|
||||
}
|
||||
|
||||
void
|
||||
catching(void)
|
||||
{
|
||||
o_text("ready to catch\n");
|
||||
if (trap(throwing)) {
|
||||
o_text("caught!\n");
|
||||
} else {
|
||||
# nothing was thrown
|
||||
}
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
catching();
|
||||
|
||||
return 0;
|
||||
}
|
||||
39
Task/Exceptions/Aime/exceptions-2.aime
Normal file
39
Task/Exceptions/Aime/exceptions-2.aime
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
void
|
||||
ft(integer a, text &s)
|
||||
{
|
||||
if (a & 1) {
|
||||
s = "odd";
|
||||
error("bad number");
|
||||
} elif (a & a - 1) {
|
||||
s = "not a power of two";
|
||||
error("bad number");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
fc(integer a)
|
||||
{
|
||||
text e;
|
||||
|
||||
if (trap(ft, a, e)) {
|
||||
v_text("exception of type `");
|
||||
v_text(e);
|
||||
v_text("' thrown for ");
|
||||
v_integer(a);
|
||||
v_newline();
|
||||
} else {
|
||||
v_text("no exception thrown for ");
|
||||
v_integer(a);
|
||||
v_newline();
|
||||
}
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
fc(5);
|
||||
fc(6);
|
||||
fc(8);
|
||||
|
||||
return 0;
|
||||
}
|
||||
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
|
||||
11
Task/Exceptions/Applesoft-BASIC/exceptions.basic
Normal file
11
Task/Exceptions/Applesoft-BASIC/exceptions.basic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
100 REM TRY
|
||||
110 ONERR GOTO 160"CATCH
|
||||
120 PRINT 1E99
|
||||
130 POKE 216,0
|
||||
140 PRINT "NEVER REACHED."
|
||||
150 GOTO 200"END TRY
|
||||
160 REM CATCH
|
||||
170 POKE 216,0
|
||||
180 LET E = PEEK (222)
|
||||
190 PRINT "ERROR "E" OCCURRED."
|
||||
200 REM END TRY
|
||||
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.basic
Normal file
10
Task/Exceptions/BBC-BASIC/exceptions.basic
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/BQN/exceptions-1.bqn
Normal file
4
Task/Exceptions/BQN/exceptions-1.bqn
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
C←{𝕊:2‿𝕩⥊2}⎊{"Exception caught"‿𝕩}
|
||||
|
||||
•Show C 3
|
||||
•Show C "dsda"
|
||||
5
Task/Exceptions/BQN/exceptions-2.bqn
Normal file
5
Task/Exceptions/BQN/exceptions-2.bqn
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
┌─
|
||||
╵ 2 2 2
|
||||
2 2 2
|
||||
┘
|
||||
⟨ "Exception caught" "dsda" ⟩
|
||||
6
Task/Exceptions/Blz/exceptions.blz
Normal file
6
Task/Exceptions/Blz/exceptions.blz
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
try
|
||||
1 / 0 # Throw an exception
|
||||
print("unreachable code")
|
||||
catch
|
||||
print("An error occured!")
|
||||
end
|
||||
17
Task/Exceptions/Bracmat/exceptions.bracmat
Normal file
17
Task/Exceptions/Bracmat/exceptions.bracmat
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
( ( MyFunction
|
||||
= someText XMLstuff
|
||||
. ( get$!arg:?someText
|
||||
& get$("CorporateData.xml",X,ML):?XMLstuff
|
||||
| out
|
||||
$ ( str
|
||||
$ ( "Something went wrong when reading your file \""
|
||||
!arg
|
||||
"\". Or was it the Corporate Data? Hard to say. Anyhow, now I throw you out."
|
||||
)
|
||||
)
|
||||
& ~
|
||||
)
|
||||
& contemplate$(!someText,!XMLstuff)
|
||||
)
|
||||
& MyFunction$"Tralula.txt"
|
||||
);
|
||||
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
|
||||
{
|
||||
virtual const char* what() const noexcept { return "description"; }
|
||||
}
|
||||
4
Task/Exceptions/C++/exceptions-3.cpp
Normal file
4
Task/Exceptions/C++/exceptions-3.cpp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
void foo()
|
||||
{
|
||||
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
|
||||
}
|
||||
4
Task/Exceptions/C-sharp/exceptions-1.cs
Normal file
4
Task/Exceptions/C-sharp/exceptions-1.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
public class MyException : Exception
|
||||
{
|
||||
// data with info about exception
|
||||
};
|
||||
4
Task/Exceptions/C-sharp/exceptions-2.cs
Normal file
4
Task/Exceptions/C-sharp/exceptions-2.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
void foo()
|
||||
{
|
||||
throw MyException();
|
||||
}
|
||||
11
Task/Exceptions/C-sharp/exceptions-3.cs
Normal file
11
Task/Exceptions/C-sharp/exceptions-3.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
try {
|
||||
foo();
|
||||
}
|
||||
catch (MyException e)
|
||||
{
|
||||
// handle exceptions of type MyException and derived
|
||||
}
|
||||
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.dw
Normal file
4
Task/Exceptions/DWScript/exceptions-1.dw
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
procedure Test;
|
||||
begin
|
||||
raise Exception.Create('Sample Exception');
|
||||
end;
|
||||
11
Task/Exceptions/DWScript/exceptions-2.dw
Normal file
11
Task/Exceptions/DWScript/exceptions-2.dw
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.dw
Normal file
8
Task/Exceptions/DWScript/exceptions-3.dw
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;
|
||||
10
Task/Exceptions/Dyalect/exceptions.dyalect
Normal file
10
Task/Exceptions/Dyalect/exceptions.dyalect
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
func Integer.Add(x) {
|
||||
throw @NegativesNotAllowed(x) when x < 0
|
||||
this + x
|
||||
}
|
||||
|
||||
try {
|
||||
12.Add(-5)
|
||||
} catch {
|
||||
@NegativesNotAllowed(x) => print("Negative number: \(x)")
|
||||
}
|
||||
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?"))
|
||||
}
|
||||
}
|
||||
5
Task/Exceptions/Elena/exceptions-1.elena
Normal file
5
Task/Exceptions/Elena/exceptions-1.elena
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class MyException : Exception
|
||||
{
|
||||
constructor new()
|
||||
<= new("MyException raised");
|
||||
}
|
||||
4
Task/Exceptions/Elena/exceptions-2.elena
Normal file
4
Task/Exceptions/Elena/exceptions-2.elena
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
foo()
|
||||
{
|
||||
MyException.raise()
|
||||
}
|
||||
8
Task/Exceptions/Elena/exceptions-3.elena
Normal file
8
Task/Exceptions/Elena/exceptions-3.elena
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
try
|
||||
{
|
||||
o.foo()
|
||||
}
|
||||
catch:(MyException e)
|
||||
{
|
||||
// handle exceptions of type MyException and derived
|
||||
}
|
||||
4
Task/Exceptions/Elena/exceptions-4.elena
Normal file
4
Task/Exceptions/Elena/exceptions-4.elena
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
o.foo() | on:(e)
|
||||
{
|
||||
// handle any type of exception
|
||||
};
|
||||
12
Task/Exceptions/Erlang/exceptions.erl
Normal file
12
Task/Exceptions/Erlang/exceptions.erl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
-module( exceptions ).
|
||||
|
||||
-export( [task/0] ).
|
||||
|
||||
task() ->
|
||||
try
|
||||
erlang:throw( new_exception )
|
||||
|
||||
catch
|
||||
_:Exception -> io:fwrite( "Catched ~p~n", [Exception] )
|
||||
|
||||
end.
|
||||
4
Task/Exceptions/Factor/exceptions-1.factor
Normal file
4
Task/Exceptions/Factor/exceptions-1.factor
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"Install Linux, Problem Solved" throw
|
||||
|
||||
TUPLE: velociraptor ;
|
||||
\ velociraptor new throw
|
||||
2
Task/Exceptions/Factor/exceptions-2.factor
Normal file
2
Task/Exceptions/Factor/exceptions-2.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ERROR: velociraptor ;
|
||||
velociraptor
|
||||
12
Task/Exceptions/Factor/exceptions-3.factor
Normal file
12
Task/Exceptions/Factor/exceptions-3.factor
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
! Preferred exception handling
|
||||
: try-foo
|
||||
[ foo ] [ foo-failed ] recover ;
|
||||
|
||||
: try-bar
|
||||
[ bar ] [ bar-errored ] [ bar-always ] cleanup ;
|
||||
|
||||
! Used rarely
|
||||
[ "Fail" throw ] try ! throws a "Fail"
|
||||
[ "Fail" throw ] catch ! returns "Fail"
|
||||
[ "Hi" print ] catch ! returns f (looks the same as throwing f; don't throw f)
|
||||
[ f throw ] catch ! returns f, bad! use recover or cleanup instead
|
||||
20
Task/Exceptions/Fancy/exceptions.fancy
Normal file
20
Task/Exceptions/Fancy/exceptions.fancy
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# define custom exception class
|
||||
# StandardError is base class for all exception classes
|
||||
class MyError : StandardError {
|
||||
def initialize: message {
|
||||
# forward to StdError's initialize method
|
||||
super initialize: message
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
# raises/throws a new MyError exception within try-block
|
||||
MyError new: "my message" . raise!
|
||||
} catch MyError => e {
|
||||
# catch exception
|
||||
# this will print "my message"
|
||||
e message println
|
||||
} finally {
|
||||
# this will always be executed (as in e.g. Java)
|
||||
"This is how exception handling in Fancy works :)" println
|
||||
}
|
||||
23
Task/Exceptions/Fantom/exceptions.fantom
Normal file
23
Task/Exceptions/Fantom/exceptions.fantom
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// Create a new error class by subclassing sys::Err
|
||||
const class SpecialErr : Err
|
||||
{
|
||||
// you must provide some message about the error
|
||||
// to the parent class, for reporting
|
||||
new make () : super ("special error") {}
|
||||
}
|
||||
|
||||
class Main
|
||||
{
|
||||
static Void fn ()
|
||||
{
|
||||
throw SpecialErr ()
|
||||
}
|
||||
|
||||
public static Void main ()
|
||||
{
|
||||
try
|
||||
fn()
|
||||
catch (SpecialErr e)
|
||||
echo ("Caught " + e)
|
||||
}
|
||||
}
|
||||
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
|
||||
30
Task/Exceptions/FreeBASIC/exceptions.basic
Normal file
30
Task/Exceptions/FreeBASIC/exceptions.basic
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Enum ErrorType
|
||||
myError = 1000
|
||||
End Enum
|
||||
|
||||
Sub foo()
|
||||
Err = 1000 ' raise a user-defined error
|
||||
End Sub
|
||||
|
||||
Sub callFoo()
|
||||
foo()
|
||||
Dim As Long errNo = Err ' cache Err in case it's reset by a different function
|
||||
Select Case errNo
|
||||
Case 0
|
||||
' No error (system defined)
|
||||
Case 1 To 17
|
||||
' System defined runtime errors
|
||||
Case myError: ' catch myError
|
||||
Print "Caught myError : Error number"; errNo
|
||||
Case Else
|
||||
' catch any other type of errors here
|
||||
End Select
|
||||
' add any clean-up code here
|
||||
End Sub
|
||||
|
||||
callfoo()
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
30
Task/Exceptions/Gambas/exceptions.gambas
Normal file
30
Task/Exceptions/Gambas/exceptions.gambas
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
Public Sub Main()
|
||||
Dim iInteger As Integer
|
||||
|
||||
MakeError
|
||||
DivError
|
||||
|
||||
iInteger = "2.54"
|
||||
|
||||
Catch
|
||||
Print Error.Text
|
||||
|
||||
End
|
||||
'______________________
|
||||
Public Sub DivError()
|
||||
|
||||
Print 10 / 0
|
||||
|
||||
Catch
|
||||
Print Error.Text
|
||||
|
||||
End
|
||||
'______________________
|
||||
Public Sub MakeError()
|
||||
|
||||
Error.Raise("My Error")
|
||||
|
||||
Catch
|
||||
Print Error.Text
|
||||
|
||||
End
|
||||
22
Task/Exceptions/Go/exceptions.go
Normal file
22
Task/Exceptions/Go/exceptions.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func foo() int {
|
||||
fmt.Println("let's foo...")
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
fmt.Println("Recovered from", e)
|
||||
}
|
||||
}()
|
||||
var a []int
|
||||
a[12] = 0
|
||||
fmt.Println("there's no point in going on.")
|
||||
panic("never reached")
|
||||
panic(fmt.Scan) // Can use any value, here a function!
|
||||
}
|
||||
|
||||
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 -}
|
||||
8
Task/Exceptions/HolyC/exceptions-1.holyc
Normal file
8
Task/Exceptions/HolyC/exceptions-1.holyc
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
try {
|
||||
U8 *err = 'Error';
|
||||
throw(err); // throw exception
|
||||
} catch {
|
||||
if (err == 'Error')
|
||||
Print("Raised 'Error'");
|
||||
PutExcept; // print the exception and stack trace
|
||||
}
|
||||
17
Task/Exceptions/HolyC/exceptions-2.holyc
Normal file
17
Task/Exceptions/HolyC/exceptions-2.holyc
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
|
||||
18
Task/Exceptions/J/exceptions.j
Normal file
18
Task/Exceptions/J/exceptions.j
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
pickyPicky =: verb define
|
||||
if. y-:'bad argument' do.
|
||||
throw.
|
||||
else.
|
||||
'thanks!'
|
||||
end.
|
||||
)
|
||||
|
||||
tryThis =: verb define
|
||||
try.
|
||||
pickyPicky y
|
||||
catcht.
|
||||
'Uh oh!'
|
||||
end.
|
||||
)
|
||||
|
||||
tryThis 'bad argument'
|
||||
Uh oh!
|
||||
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/Jq/exceptions-1.jq
Normal file
1
Task/Exceptions/Jq/exceptions-1.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
try FILTER catch CATCHER
|
||||
14
Task/Exceptions/Jq/exceptions-2.jq
Normal file
14
Task/Exceptions/Jq/exceptions-2.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
def division(a;b):
|
||||
def abs: if . < 0 then -. else . end;
|
||||
if a == 0 and b == 0 then error("0/0")
|
||||
elif b == 0 then error("division by 0")
|
||||
elif (a|abs|log) - (b|abs|log) > 700 then error("OOB")
|
||||
else a/b
|
||||
end;
|
||||
|
||||
def test(a;b):
|
||||
try division(a;b)
|
||||
catch if . == "0/0" then 0
|
||||
elif . == "division by 0" then null
|
||||
else "\(.): \(a) / \(b)"
|
||||
end;
|
||||
14
Task/Exceptions/Julia/exceptions.julia
Normal file
14
Task/Exceptions/Julia/exceptions.julia
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function extendedsqrt(x)
|
||||
try sqrt(x)
|
||||
catch
|
||||
if x isa Number
|
||||
sqrt(complex(x, 0))
|
||||
else
|
||||
throw(DomainError())
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@show extendedsqrt(1) # 1
|
||||
@show extendedsqrt(-1) # 0.0 + 1.0im
|
||||
@show extendedsqrt('x') # ERROR: DomainError
|
||||
23
Task/Exceptions/Kotlin/exceptions.kotlin
Normal file
23
Task/Exceptions/Kotlin/exceptions.kotlin
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// version 1.0.6
|
||||
|
||||
// In Kotlin all Exception classes derive from Throwable and, by convention, end with the word 'Exception'
|
||||
class MyException (override val message: String?): Throwable(message)
|
||||
|
||||
fun foo() {
|
||||
throw MyException("Bad foo!")
|
||||
}
|
||||
|
||||
fun goo() {
|
||||
try {
|
||||
foo()
|
||||
}
|
||||
catch (me: MyException) {
|
||||
println("Caught MyException due to '${me.message}'")
|
||||
println("\nThe stack trace is:\n")
|
||||
me.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
goo()
|
||||
}
|
||||
14
Task/Exceptions/Langur/exceptions-1.langur
Normal file
14
Task/Exceptions/Langur/exceptions-1.langur
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# do something
|
||||
throw "not a math exception"
|
||||
|
||||
catch .e {
|
||||
if .e["cat"] == "math" {
|
||||
# change result...
|
||||
} else {
|
||||
# rethrow the exception
|
||||
throw
|
||||
}
|
||||
} else {
|
||||
# no exception
|
||||
...
|
||||
}
|
||||
10
Task/Exceptions/Langur/exceptions-2.langur
Normal file
10
Task/Exceptions/Langur/exceptions-2.langur
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
100 / 0
|
||||
|
||||
catch {
|
||||
if _err["cat"] == "math" {
|
||||
# change result
|
||||
123
|
||||
} else {
|
||||
throw
|
||||
}
|
||||
}
|
||||
3
Task/Exceptions/Langur/exceptions-3.langur
Normal file
3
Task/Exceptions/Langur/exceptions-3.langur
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
val .safediv = f { .x / .y ; catch { 0 } }
|
||||
.safediv(7, 7) # 1
|
||||
.safediv(7, 0) # 0
|
||||
6
Task/Exceptions/Lasso/exceptions.lasso
Normal file
6
Task/Exceptions/Lasso/exceptions.lasso
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
protect => {
|
||||
handle_error => {
|
||||
// do something else
|
||||
}
|
||||
fail(-1,'Oops')
|
||||
}
|
||||
19
Task/Exceptions/Lingo/exceptions-1.lingo
Normal file
19
Task/Exceptions/Lingo/exceptions-1.lingo
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
-- parent script "ErrorHandler"
|
||||
|
||||
on alertHook (me, errorType, errorMessage, alertType)
|
||||
if alertType=#alert then return 0 -- ignore programmatic alerts
|
||||
|
||||
-- log error in file "error.log"
|
||||
fn = _movie.path&"error.log"
|
||||
fp = xtra("fileIO").new()
|
||||
fp.openFile(fn, 2)
|
||||
if fp.status() = -37 then
|
||||
fp.createFile(fn)
|
||||
fp.openFile(fn, 2)
|
||||
end if
|
||||
fp.setPosition(fp.getLength())
|
||||
fp.writeString(_system.date() && _system.time() && errorType & ": " & errorMessage & RETURN)
|
||||
fp.closeFile()
|
||||
|
||||
return 1 -- continues movie playback, no error dialog
|
||||
end
|
||||
4
Task/Exceptions/Lingo/exceptions-2.lingo
Normal file
4
Task/Exceptions/Lingo/exceptions-2.lingo
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
-- in a movie script
|
||||
on prepareMovie
|
||||
_player.alertHook = script("ErrorHandler")
|
||||
end
|
||||
6
Task/Exceptions/Lingo/exceptions-3.lingo
Normal file
6
Task/Exceptions/Lingo/exceptions-3.lingo
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
-- in a movie script
|
||||
-- usage: throw("Custom error 23")
|
||||
on throw (msg)
|
||||
_player.alertHook.alertHook("Script runtime error", msg, #script)
|
||||
abort() -- exits call stack
|
||||
end
|
||||
7
Task/Exceptions/Logo/exceptions.logo
Normal file
7
Task/Exceptions/Logo/exceptions.logo
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
to div.checked :a :b
|
||||
if :b = 0 [(throw "divzero 0)]
|
||||
output :a / :b
|
||||
end
|
||||
to div.safely :a :b
|
||||
output catch "divzero [div.checked :a :b]
|
||||
end
|
||||
23
Task/Exceptions/Logtalk/exceptions.logtalk
Normal file
23
Task/Exceptions/Logtalk/exceptions.logtalk
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
:- object(exceptions).
|
||||
|
||||
:- public(double/2).
|
||||
double(X, Y) :-
|
||||
catch(double_it(X,Y), Error, handler(Error, Y)).
|
||||
|
||||
handler(error(not_a_number(X), logtalk(This::double(X,Y), Sender)), Y) :-
|
||||
% try to fix the error and resume computation;
|
||||
% if not possible, rethrow the exception
|
||||
( catch(number_codes(Nx, X), _, fail) ->
|
||||
double_it(Nx, Y)
|
||||
; throw(error(not_a_number(X), logtalk(This::double(X,Y), Sender)))
|
||||
).
|
||||
|
||||
double_it(X, Y) :-
|
||||
( number(X) ->
|
||||
Y is 2*X
|
||||
; this(This),
|
||||
sender(Sender),
|
||||
throw(error(not_a_number(X), logtalk(This::double(X,Y), Sender)))
|
||||
).
|
||||
|
||||
:- end_object.
|
||||
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)
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue