Exception Handling Previous discussion focuses on normal control

  • Slides: 4
Download presentation
Exception Handling • Previous discussion focuses on normal control flow – Sometimes program reaches

Exception Handling • Previous discussion focuses on normal control flow – Sometimes program reaches a point where it cannot continue correctly and in a principled manner – Exception handling provides a type-safe way to manage control of such exceptional cases – Often less efficient, so use sparingly and only when needed • Exception types and semantics may vary – – Some languages provide built-in exception types Others (e. g. , C++11) introduce them via libraries Syntax and semantics also varies between languages E. g. , C++ has an explicit try/catch construct whereas in other languages you can attach handlers to arbitrary blocks – Exceptions usually propagate if uncaught, unwind stack frames, and may even terminate main function of a program CSE 425: Control Abstraction II

Exception Handling • Raising/handling exceptions similar to procedure calls – But, stack unwinds, so

Exception Handling • Raising/handling exceptions similar to procedure calls – But, stack unwinds, so can’t put activation record there – Need to find/call handler dynamically • One approach is to keep a separate handler stack – Nicely general but may be expensive to maintain at runtime – May be necessary to avoid restricting handler semantics • C++ pre-computes address-indexed dispatch table – Avoids any cost to code that doesn’t use exceptions – Still somewhat expensive since it needs (e. g. , binary) search – Search also motivates first-matching-catch-block semantics CSE 425: Control Abstraction II

Coroutines and Events • Coroutines offer an alternative to nested procedures – Take turns

Coroutines and Events • Coroutines offer an alternative to nested procedures – Take turns executing (cooperatively alternating) – Detach operation establishes ability to transfer control – Transfer operation saves program counter in a routine, transfers control to current point of execution in another • Coroutines offer a natural approach to event handling – Originated in Simula (discrete event simulation language) – Iterators can use coroutines (but often done more simply) • In general, event handling involves independent code – Handling key press vs. mouse move vs. network packet … – The idea is to abstract handlers for each distinct event and then coordinate their operations (e. g. , clicking on a window brings it to the foreground and directs subsequent input to it) CSE 425: Control Abstraction II

Today’s Studio Exercises • We’ll code up ideas from Scott Chapter 8. 5 -8.

Today’s Studio Exercises • We’ll code up ideas from Scott Chapter 8. 5 -8. 8 – Looking primarily at exceptions (optionally event handling) • Today’s exercises are again in C++ – Please take advantage of the on-line tutorial and reference manual pages that are linked on the course web site – As always, please ask us for help as needed • When done, email your answers with subject line“Control Abstraction Studio II” to cse 425@seas. wustl. edu CSE 425: Control Abstraction II