| 1 | = Try/Catch = |
| 2 | |
| 3 | A statement that specifies to run (try) a block of code and to trap (catch) and handle |
| 4 | any exceptions thrown from it. |
| 5 | |
| 6 | Exceptions can be specified to be trapped in one of three ways |
| 7 | * catch exception type and capture the exception instance for use in its corresponding catch block. |
| 8 | * catch exception type ignoring actual instance |
| 9 | * catchAll clause - catch any exceptions not already given |
| 10 | |
| 11 | The '''success''' clause is optional and if specified is executed only if no catch clauses |
| 12 | are invoked (i.e if no catch specified exceptions are thrown). |
| 13 | |
| 14 | The '''finally''' clause is also optional and if specified is invoked after all other |
| 15 | clauses regardless of whether any exceptions were thrown, trapped and/or rethrown. |
| 16 | Control is always passed to any finally block regardless of how the try block exits. |
| 17 | |
| 18 | Whereas catch clauses are used to handle exceptions that occur in a statement block, |
| 19 | '''finally''' is used to guarantee a statement block of code executes regardless of how the |
| 20 | preceding try block is exited. Used for must-cleanup situations (see also [wiki:Using Using]). |
| 21 | |
| 22 | |
| 23 | == Grammar == |
| 24 | {{{ |
| 25 | try |
| 26 | BLOCK |
| 27 | catch VARNAME as EXCEPTIONTYPE (*1) or |
| 28 | catch EXCEPTIONTYPE (*2) or |
| 29 | catch (*3) |
| 30 | CATCH_BLOCK |
| 31 | |
| 32 | [success (*4) |
| 33 | SUCCESS_BLOCK ] |
| 34 | |
| 35 | [finally (*4) |
| 36 | FINALLY_BLOCK ] |
| 37 | |
| 38 | }}} |
| 39 | *1 - specify a variable to place the caught exception in to manipulate (or rethrow) |
| 40 | within the subsequent catch codeblock |
| 41 | |
| 42 | *2 - Use this form to catch a particular exceptionType when dont need the particular |
| 43 | exception instance. (To ignore the exception or exit for example) |
| 44 | |
| 45 | *3 - Catch any and all exceptions not already specifically listed (!CatchAll clause) |
| 46 | There can be only one catchAll clause and if used it must be after all |
| 47 | other catch clauses. |
| 48 | |
| 49 | *4 - optional |
| 50 | |
| 51 | == Platform == |
| 52 | |
| 53 | == Examples == |
| 54 | {{{ |
| 55 | |
| 56 | # parsing numbers |
| 57 | s = tok.text.replace('_', '') |
| 58 | if s.endsWith('f') |
| 59 | s = s[:-1] |
| 60 | try |
| 61 | value = float.parse(s, Utils.cultureInfoForNumbers) |
| 62 | catch FormatException |
| 63 | .recordError('Unexpected FormatException for float literal "[tok.text]".') |
| 64 | catch OverflowException |
| 65 | .recordError('Range overflow for float literal "[tok.text]".') |
| 66 | |
| 67 | |
| 68 | # reading file contents |
| 69 | contents = 'test: failure' |
| 70 | try |
| 71 | contents = File.readAllText('myFile.txt') |
| 72 | catch ioe as IOException |
| 73 | print 'I/O Error: [ioe.message]' |
| 74 | success |
| 75 | assert 'test' in contents |
| 76 | |
| 77 | # customException |
| 78 | try |
| 79 | if source.length==0 |
| 80 | _warning('File is empty.') |
| 81 | else if source.trim.length==0 |
| 82 | _warning('File is completely blank.') |
| 83 | return _parseTokens() |
| 84 | catch pe as ParserException |
| 85 | if _errorRecorder, _errorRecorder.recordError(pe) |
| 86 | _tokenizer = nil |
| 87 | throw # rethrow exception trapped above |
| 88 | }}} |
| 89 | |
| 90 | |
| 91 | == Notes == |