exception handling
play

Exception handling Exception infrequent, abnormal situation in - PowerPoint PPT Presentation

Exception handling Exception infrequent, abnormal situation in program logic at program execution not necessarily an error Reasons for exceptions: hardware events arithmetic overflow, parity errors operating


  1. Exception handling • Exception – infrequent, abnormal situation in program logic at program execution – not necessarily an error • Reasons for exceptions: – hardware events • arithmetic overflow, parity errors – operating system events • out of memory exception – programming language properties • dynamic range checks – application program properties • subprogram abuse, data structure overflow/underflow TUT Pervasive Computing Principles of programming languages 1 Maarit Harsu / Matti Rintala / Henri Hansen

  2. protected block exception handler Exception handling raise/throw an exception • Necessary actions to continue program execution after detecting an exception – reporting about the exception – controlled program termination • Exception recovery (different choices) – new trial • e.g. after restoring data structures – alternative action – memory allocation – ask for instructions TUT Pervasive Computing Principles of programming languages 2 Maarit Harsu / Matti Rintala / Henri Hansen

  3. Tools for reporting exceptions • Using parameters (or global variables): procedure Search ( K: Key; var D: Info; var Found: Boolean ); begin ... end ; ... Search ( k, d, f ); if not f then ... exception handling ... • Using a special exceptional value: procedure Search ( K: Key; var D: Info ); begin ... end ; ... Search ( k, d ); if d = NotFound then ... exception handling ... TUT Pervasive Computing Principles of programming languages 3 Maarit Harsu / Matti Rintala / Henri Hansen

  4. Comparison to Go func Open ( name string ) ( file *File, err error ) f, err := os.Open ( ”filename.txt” ) if err != nil { log.Fatal ( err ) // prints error message and stops } // do something with the open *File f TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

  5. Tools for giving instructions • Passing a label as a parameter: procedure Search ( K: Key; var D: Info; label NotFound ); begin ... if ...not found ... then goto NotFound; end ; ... Search ( k, d, L ); // and jump over L L: ... exception handling ... • Passing a handler routine as a parameter: procedure Search ( K: Key; var D: Info; procedure NotFound ); begin ... if ...not found ... then NotFound; end ; procedure P; begin ... exception handling ... end ; ... TUT Pervasive Computing Principles of programming languages 5 Search ( k, d, P ); Maarit Harsu / Matti Rintala / Henri Hansen

  6. Benefits of language-provided exception handling • Specific concepts for exception handling – high-level abstraction • Exceptional situations distinguished from the normal program logic – readability, understandability, (orthogonality) • Efficiency? – does not burden normal program execution • Handling mechanism also for system exceptions TUT Pervasive Computing Principles of programming languages 6 Maarit Harsu / Matti Rintala / Henri Hansen

  7. Design issues for exception handling • How to detect an exception? How to handle hardware exceptions? • How, when and where to report about the exception? How to select a handler? • How to bind an exception and its handler together? – how is the handler related to the code sequence where its exceptions occur? • What to do after the exception is handled? TUT Pervasive Computing Principles of programming languages 7 Maarit Harsu / Matti Rintala / Henri Hansen

  8. Ada • Exceptions are built-in types declare empty_queue: exception ; • Handlers are associated statically with large program units – blocks, subprograms, packages, tasks – handler is bound dynamically (after subprogram calling order) • Multi-level handling – moving backwards in the chain of called subprograms until the suitable handler is found • Exception can also be raised in an exception handler TUT Pervasive Computing Principles of programming languages 8 Maarit Harsu / Matti Rintala / Henri Hansen

  9. Example on Ada package Stack is procedure Push ( X: Integer ); exception handling function Pop return Integer; Underflow, Overflow: exception ; Location for handler: end Stack; 1. Block 2. Subprogram body package body Stack is 3. Package body max: constant := 10; 4. Task body S: array ( 1.. max ) of Integer; top: Integer range 0 .. max; declare procedure Push ( X: Integer ) is use Stack; begin begin if top = max then ... raise Overflow; Push ( 3 ); end if ; ... -- actual code for Push exception end Push; when Underflow | Overflow => function Pop return Integer is Put ( ”Error in stack handling” ); ... raise Underflow; ... end ; end Stack; TUT Pervasive Computing Principles of programming languages 9 Maarit Harsu / Matti Rintala / Henri Hansen

  10. Binding handler to an exception (Ada) • Moving exception (until suitable handler is found) – from subprogram body • the same exceptions is raised again in the call of the subprogram – from block • the same exception is raised again after the block – from package body • the same exception is raised again after the body – from task body • not moved, task is terminated – from declarations • elaboration of declarations is terminated TUT Pervasive Computing Principles of programming languages 10 Maarit Harsu / Matti Rintala / Henri Hansen

  11. procedure P is Error: exception ; Control flow procedure R; procedure Q is in different begin R; situations ... -- raising Error ( 2 ) exception (Ada) when Error => ... ; end Q; procedure R is begin ... – raising Error ( 3 ) end R; begin ... -- raising Error ( 1 ) Q; ... exception when Error => ... ; TUT Pervasive Computing Principles of programming languages 11 end P; Maarit Harsu / Matti Rintala / Henri Hansen

  12. Predefined exceptions (Ada) • Constraint_Error: – restriction (e.g. range restriction) is broken • Storage_Error: – memory exhaustion • Tasking_Error: – error in tasks • Program_Error: – other error occurs TUT Pervasive Computing Principles of programming languages 12 Maarit Harsu / Matti Rintala / Henri Hansen

  13. do_task is local times_tried: INTEGER Exception do if times_tried = 0 then handling implementation1 elsif times_tried = 1 then in Eiffel implementation2 end rescue times_tried := times_tried + 1; if times_tried < 2 then ... repairing data structures ... retry end end ; TUT Pervasive Computing Principles of programming languages 13 Maarit Harsu / Matti Rintala / Henri Hansen

  14. C++ • Exception terminology: – try -block • includes the error-prone code (protected block) – throw -command • throws (raises) an exception – catch -block • handles the exception • Exception – can be a data element or object of any type – does not necessarily have a name – exception class TUT Pervasive Computing Principles of programming languages 14 Maarit Harsu / Matti Rintala / Henri Hansen

  15. int space_left ( int index ) { if ( index >= MAX ) throw ( index ); C++ else return 1; } int main ( void ) { int array [ MAX ]; int i = 0; try { while ( space_left ( i ) && ( cin >> array [ i ] ) ) i++; } catch ( int i ) { cout << ”Index ” << i << ”out of range\n”; } return 0; } TUT Pervasive Computing Principles of programming languages 15 Maarit Harsu / Matti Rintala / Henri Hansen

  16. int const SIZE = 10; C++ int table [ SIZE ]; (exception class) int InsideTable ( int i ) { if ( i >= SIZE ) throw OverIndex ( i ); else return 1; } void main ( ) { int ind; class OverIndex { try { private : ind = 0; int ind; while ( InsideTable ( ind ) ) public : cin >> table [ ind++ ]; OverIndex ( int i ) { ind = i; } } void report ( ) { catch ( OverIndex error ) { cout << ”Bad index: ” << ind; error.report ( ); } } }; } TUT Pervasive Computing Principles of programming languages 16 Maarit Harsu / Matti Rintala / Henri Hansen

  17. C++ standard exception hierarchy out_of_range bad_alloc length_error exception bad_typeid domain_error logic_error invalid_argument bad_exception range_error bad_cast overflow_error runtime_error underflow_error TUT Pervasive Computing Principles of programming languages 17 Maarit Harsu / Matti Rintala / Henri Hansen

  18. Java • Resembles C++ exception handling • Exeptions are instances of Throwable – predefined exception classes: Error and Exception and their subclasses • Exceptions must be given in the header of an operation • finally -clause (after try and catch blocks) – is executed always (regardless of exceptions) try { ... } catch { ... } // more handlers TUT Pervasive Computing Principles of programming languages 18 finally { ... } Maarit Harsu / Matti Rintala / Henri Hansen

  19. Haskell • Lazy evaluation -> execution order ”undefined” -> ??? • Exceptions are problematic – when to throw, when to catch • Haskell allows exceptions only in IO functions (where order is predefined) • Other mechanisms must be used in the purely functional side TUT Pervasive Computing Principles of programming languages 19 Maarit Harsu / Matti Rintala / Henri Hansen

  20. Haskell • Function error prints an error message and terminates the program (its value is undefined) Div1 :: Int -> Int -> Int Div1 _ 0 = error ”Division by zero” Div1 x y = div x y • No error handling or recovery possible! TUT Pervasive Computing Principles of programming languages 20 Maarit Harsu / Matti Rintala / Henri Hansen

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend