Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions Handling Exceptionally Sticky Problems.

Similar presentations


Presentation on theme: "Exceptions Handling Exceptionally Sticky Problems."— Presentation transcript:

1 Exceptions Handling Exceptionally Sticky Problems

2 Handling Errors Some things can't be done

3 Guarding Can protect against foreseeable errors: – ALWAYS validate user input

4 Lack Of Context Low level code – How should it respond?

5 Lack Of Context What should we do with a bad zip code?

6 Proper Response Proper response to low level errors depends on high level code – Is this a GUI app? – Unattended server application?

7 Option 1 - Assert assert( expression ) If expression is not true, self-destruct with message

8 Option 2 - Return Devise special error code to return

9 Option 2 - Return Not always an option… – Any possible int might be a valid return value

10 Extra parameters Can add extra parameters for error code… Yuck

11 Option 3 - Exceptions Exceptions : alternative return mechanism – Way for code to return an error Indicate error by throwing a value: – Does not have to match return type

12 Catching A thrown exception will blow up your program…

13 Catching …unless you catch it try : Try this code… something bad might happen catch : Here is how to handle any exceptions – Only run if an exception thrown in try

14 Catch Catch specifies – Type of thing it catches Only catches that type – What it will call the thing it caught Use as variable inside catch

15 Catch Wrong Type == No Catch Can have multiple catches: catch(…) Catches anything - But can't use it as variable

16 Defining Exceptions Exception can be anything – Custom type:

17 Stack Unwinding Call stack stores how we got to a line of code:

18 Stack Unwinding Thrown exception works back down stack looking for a catch – Does function C catch? – No, how bout B? – No, how bout A? – No, how bout Main?

19 Std::Excptions Std library defines exception class exception is parent to all – Has virtual what() function http://www.cplusplus.com/reference/exception/exception/

20 Exception Subclasses Many subclasses to exception – All support what() – All can be caught as exception

21 Exception Subclasses Can't add information to plain exception: Sub classes – Allow for construction with string message – Help specify problem

22 Proper Catching Using out_of_range exception class:

23 Reacting to Exceptions Choices for dealing with exceptions – Fix the error – Log & Continue – Blow up But get to make decision at appropriate level

24 Announcing a Throw Can announce what your function throws: – I throw nothing: – I thrown exception (or subtypes) – I throw these two types

25 What programmers want Programmers think this means: – Announce to other programmers what to expect – Check at compile time that someone will catch – Speed things up by not worrying about other types

26 What compiler does Compiler does: – Check at compile time that someone will catch Blow up program at run time if it throws something else – Speed things up by not worrying about other types Probably slow things down by doing checks at runtime

27 What Programmers Need Use comments to announce what to expect: – Announce to other programmers what to expect – Check at compile time that someone will catch – Speed things up by not worrying about other types

28 Final Thoughts When to throw – Exceptional problem you can't handle at this level – Still try to prevent errors at higher level What to throw – Approproiate std exception (i.e. out_of_range) – std::logic_error : good catch all


Download ppt "Exceptions Handling Exceptionally Sticky Problems."

Similar presentations


Ads by Google