exceptions handling exceptionally sticky problems

28
Exceptions Handling Exceptionally Sticky Problems

Upload: kory-rodgers

Post on 02-Jan-2016

232 views

Category:

Documents


3 download

TRANSCRIPT

Exceptions

Handling Exceptionally Sticky Problems

Handling Errors

• Some things can't be done

Guarding

• Can protect against foreseeable errors:– ALWAYS validate user input

Lack Of Context

• Low level code– How should it respond?

Lack Of Context

• What should we do with a bad zip code?

Proper Response

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

Option 1 - Assert

• assert( expression ) If expression is not true, self-destruct with message

Option 2 - Return

• Devise special error code to return

Option 2 - Return

• Not always an option…– Any possible int might be a valid return value

Extra parameters

•Can add extra parameters for errorcode…

•Yuck

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

Catching

• A thrown exception will blow up your program…

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

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

Catch

• Wrong Type == No Catch• Can have multiple catches:

catch(…) • Catches anything - But can't use it as variable

Defining Exceptions

• Exception can be anything– Custom type:

Stack Unwinding

• Call stack stores how we got to a line of code:

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?

Std::Excptions

• Std library defines exception class

• exception is parent to all– Has virtual what() function

http://www.cplusplus.com/reference/exception/exception/

Exception Subclasses

• Many subclasses to exception– All support what()– All can be caught as exception

Exception Subclasses

• Can't add information to plain exception:

• Sub classes– Allow for construction with string message– Help specify problem

Proper Catching

• Using out_of_range exception class:

Reacting to Exceptions

• Choices for dealing with exceptions– Fix the error– Log & Continue– Blow up

• But get to make decision at appropriate level

Announcing a Throw

• Can announce what your function throws:– I throw nothing:

– I thrown exception (or subtypes)

– I throw these two types

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

What compiler does

• Compiler does:

– Check at compile time that someone will catchBlow up program at run time if it throws something else

– Speed things up by not worrying about other typesProbably slow things down by doing checks atruntime

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

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