Download presentation
Presentation is loading. Please wait.
Published byClifton Dalton Modified over 8 years ago
1
CSE 425: Control Abstraction II 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
2
CSE 425: Control Abstraction II 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
3
CSE 425: Control Abstraction II 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)
4
CSE 425: Control Abstraction II 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 cse425@seas.wustl.edu
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.