Exceptions: Difference between revisions
Content added Content deleted
(C++ removed deprecated throw() specification; reworded) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 2,219: | Line 2,219: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
|||
Phix provides try/catch and throw statements. |
Phix provides try/catch and throw statements. |
||
'''Throwing exceptions'''<br> |
'''Throwing exceptions'''<br> |
||
You can throw any string (on it's own) or any integer, optionally with any (deeply nested) user_data that you like. |
You can throw any string (on it's own) or any integer, optionally with any (deeply nested) user_data that you like. |
||
<lang Phix> |
<!--<lang Phix>--> |
||
<span style="color: #008080;">throw<span style="color: #0000FF;">(<span style="color: #008000;">"oh no"<span style="color: #0000FF;">)</span> |
|||
throw(1) |
|||
<span style="color: #008080;">throw<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">)</span> |
|||
throw(501,{"she",made[me],Do(it)})</lang> |
|||
<span style="color: #008080;">throw<span style="color: #0000FF;">(<span style="color: #000000;">501<span style="color: #0000FF;">,<span style="color: #0000FF;">{<span style="color: #008000;">"she"<span style="color: #0000FF;">,<span style="color: #000000;">made<span style="color: #0000FF;">[<span style="color: #000000;">me<span style="color: #0000FF;">]<span style="color: #0000FF;">,<span style="color: #000000;">Do<span style="color: #0000FF;">(<span style="color: #000000;">it<span style="color: #0000FF;">)<span style="color: #0000FF;">}<span style="color: #0000FF;">) |
|||
⚫ | |||
'''Catching exceptions'''<br> |
'''Catching exceptions'''<br> |
||
There is one and only one non-optional catch clause per try statement. <br> |
There is one and only one non-optional catch clause per try statement. <br> |
||
The variable caught is a sequence, augmented with run-time diagnostics, with whatever was thrown in e[E_CODE] and/or e[E_USER]. |
The variable caught is a sequence, augmented with run-time diagnostics, with whatever was thrown in e[E_CODE] and/or e[E_USER]. |
||
<lang Phix> |
<!--<lang Phix>--> |
||
<span style="color: #008080;">try</span> |
|||
one_of(these) |
|||
<span style="color: #000000;">one_of<span style="color: #0000FF;">(<span style="color: #000000;">these<span style="color: #0000FF;">)</span> |
|||
catch e |
|||
<span style="color: #008080;">catch</span> <span style="color: #000000;">e</span> |
|||
if e[E_CODE]=501 then |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">e<span style="color: #0000FF;">[<span style="color: #000000;">E_CODE<span style="color: #0000FF;">]<span style="color: #0000FF;">=<span style="color: #000000;">501</span> <span style="color: #008080;">then</span> |
|||
puts(1,"that's no excuse!\n") |
|||
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"that's no excuse!\n"<span style="color: #0000FF;">)</span> |
|||
else |
|||
<span style="color: #008080;">else</span> |
|||
throw(e) |
|||
<span style="color: #008080;">throw<span style="color: #0000FF;">(<span style="color: #000000;">e<span style="color: #0000FF;">)</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
⚫ | |||
<span style="color: #008080;">end</span> <span style="color: #008080;">try |
|||
<!--</lang>--> |
|||
An uncaught exception terminates the program in error, otherwise control resumes in the catch clause or after the end try, |
An uncaught exception terminates the program in error, otherwise control resumes in the catch clause or after the end try, |
||
with no means (apart from some really nasty inline assembly) of resuming any half-finished block of code, and indeed any |
with no means (apart from some really nasty inline assembly) of resuming any half-finished block of code, and indeed any |
||
call stack entries between a throw and a catch will already have been torn down and thrown away.<br> |
call stack entries between a throw and a catch will already have been torn down and thrown away.<br> |
||
Traditionally fatal errors are re-routed via throw() when the presence of an exception handler is detected.<br> |
Traditionally fatal errors are re-routed via throw() when the presence of an exception handler is detected.<br> |
||
There is no finally construct |
There is no absurdly confusing finally construct - trivial to <del>mimic</del> better with the introduction of a single simple boolean flag anyway. |
||
=={{header|PHL}}== |
=={{header|PHL}}== |