ENERGY 211 CME 211 - PowerPoint PPT Presentation

About This Presentation
Title:

ENERGY 211 CME 211

Description:

In C, standard library functions indicate errors by returning ... What goes on under the hood. Function calls. Memory management. Linking and loading ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 9
Provided by: lamb8
Learn more at: https://web.stanford.edu
Category:
Tags: cme | energy | hood

less

Transcript and Presenter's Notes

Title: ENERGY 211 CME 211


1
ENERGY 211 / CME 211
  • Lecture 24
  • November 14, 2008

2
Exceptions
  • In C, standard library functions indicate errors
    by returning appropriate numeric values
  • Retrieving useful information about the error is
    cumbersome
  • So is recovering from errors that occur several
    function calls deep
  • C allows throwing exceptions to facilitate
    error reporting and recovery

3
Throwing Exceptions
  • Use the throw keyword
  • throw object
  • The thrown object can be any object that can be
    copied
  • So far we have used an object of type
    stdexception
  • Can create your own exception class that contains
    information about the error leading to the
    exception

4
Consequences
  • throw statement diverts execution outside
    smallest enclosing try block
  • If no try block inside current function, calling
    function is checked, and so on
  • Variables in each scope de-allocated
  • Dynamically allocated memory is not de-allocated,
    so don't lose track of it!
  • Once an error is detected, perform any necessary
    cleanup before throwing

5
Catching Exceptions
  • Once a try block is found, execution proceeds
    with following catch blocks
  • Argument list of each catch block is checked
    against thrown object
  • If a match is found, statements in catch block
    are executed
  • Thrown object is a local variable within the
    catch block where it is declared
  • catch() matches anything

6
Exception Do's and Don'ts
  • DO throw an exception within a constructor if
    anything goes wrong
  • DO NOT throw an exception within a destructor
    might crash program!
  • DO NOT let an exception go uncaught, or execution
    will terminate immediately
  • DO NOT use throw specifications like
  • type fname(args) throw(type) because they limit
    flexibility

7
stdruntime_error
  • Instead of throwing an object of type
    stdexception, can use the derived class
    stdruntime_error
  • Declared in ltstdexceptgt header
  • Construct with your own error message
  • using namespace std
  • runtime_error ex("Idiot!")
  • throw ex
  • When caught ex.what() is a const char array with
    the error message

8
Next Time
  • Basics of Computer Organization
  • What goes on under the hood
  • Function calls
  • Memory management
  • Linking and loading
Write a Comment
User Comments (0)
About PowerShow.com