#try { block1 } [ #catch type { block2 } ][ #finally { block3 } ]
Executes block1
. If any exceptions are thrown, the #catch
sections are searched for one where the type of exception matches the type
element; if one is found, its block2
is run, if not the exception ‘escapes’. If a #finally
section is present, block3
is always executed, after block1
and whichever block2
is run, if any.
Any number of #catch
segments can be present; they are searched in order and therefore should grade from specific to general. You can stop any exceptions escaping by having a #catch $Exception {}
section.
The exception caught is stored in #exception
and is accessible within the #catch
block. You can use #throw #exception
to rethrow it (mimicing C#’s niladic throw
).
For example:
#try { sr←$IO.File:OpenText(⍵); res←[""]; #while{true} { thisline←sr:ReadLine(); #if(thisline≡#null) { #break } #else {res,←thisline} } } #catch $IO.FileNotFoundException { ⍇"File "+⍵+" was not found"; } #finally { sr:Close(); };