Wiki

Changes between Initial Version and Version 1 of Try

Show
Ignore:
Timestamp:
02/15/10 01:18:55 (15 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Try

    v1 v1  
     1= Try/Catch =  
     2 
     3A statement that specifies to run (try) a block of code and to trap (catch) and handle  
     4any exceptions thrown from it. 
     5 
     6Exceptions 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     
     11The '''success''' clause is optional and if specified is executed only if no catch clauses  
     12are invoked (i.e if no catch specified exceptions are thrown). 
     13 
     14The '''finally''' clause is also optional and if specified is invoked after all other  
     15clauses regardless of whether any exceptions were thrown, trapped and/or rethrown. 
     16Control is always passed to any finally block regardless of how the try block exits. 
     17 
     18Whereas 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  
     20preceding try block is exited. Used for must-cleanup situations (see also [wiki:Using Using]).  
     21 
     22 
     23== Grammar == 
     24{{{ 
     25try 
     26    BLOCK 
     27catch VARNAME as EXCEPTIONTYPE  (*1)   or 
     28catch EXCEPTIONTYPE             (*2)   or 
     29catch                           (*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 
     57s = tok.text.replace('_', '') 
     58if s.endsWith('f') 
     59    s = s[:-1] 
     60try 
     61    value = float.parse(s, Utils.cultureInfoForNumbers) 
     62catch FormatException 
     63    .recordError('Unexpected FormatException for float literal "[tok.text]".') 
     64catch OverflowException 
     65    .recordError('Range overflow for float literal "[tok.text]".') 
     66 
     67 
     68# reading file contents 
     69contents = 'test: failure' 
     70try 
     71    contents = File.readAllText('myFile.txt') 
     72catch ioe as IOException 
     73    print 'I/O Error: [ioe.message]' 
     74success 
     75    assert 'test' in contents 
     76     
     77# customException 
     78try 
     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() 
     84catch pe as ParserException 
     85    if _errorRecorder, _errorRecorder.recordError(pe) 
     86    _tokenizer = nil 
     87    throw  # rethrow exception trapped above 
     88}}} 
     89 
     90 
     91== Notes ==