Class Notes (838,403)
Canada (510,881)
CMPT 225 (60)
John Edgar (28)
Lecture 4

CMPT 225 Week 4 Lecture 2

2 Pages
Unlock Document

Computing Science
CMPT 225
John Edgar

Error Handling 3 basic ways of dealing with poping from an empty stack: Obviously can't return thing at top of stack. If you'd return a pointer, could return a null pointer, but might not work. If stack could hold all possible int values, -1 would not be a valid error code, so you'd have to do something else. (returning signal/default value[how do you distringuish?]) Could do reference parameter With this kind of error, guessing what you should return is generally a bad idea! You are usually much better off saying "Something has gone wrong." Done via exception handling. Different from returning some error code through a reference parameter. Using the reference parameter for error code: MyStack::pop(int &e) { ... e = 1 ... } // set e to 1 when error occurs void processNext(MyStack st) { int errorCode = 0; x = st.pop(e); if (errorCode == 1) { // problematic to deal with error here, as you might not know what these values are going to be used for/ might have to pass signal value up for a long time before it can be handled, which will almost certainly go wrong somewhere. Have to stop things in an arbitrary way. ... } } Appropriate place for handling errors is probably where processNext is being called, then you can respond there appropriately try { processNext(st); } catch ( runtime_error e) { // type of error you are expecting to recieve ...// deal with error here } 2 stages: 1. Throwing an error Causes an error to be raised. Makes your program stop working intentionally! Terminating is better than guessing when the program has no idea what to do, and could potentially do all sorts of awful things. Crashing is often better than incorrect results (think medical imaging programs, air traffic control system, etc)! Compiler errors are better than run time errors are better than logic errors. 2. Catching/handling an error != returning. Unwinding the stack, don't return from function call, just get rid of it entirely and go straight back to catch. Allows you to handle errors in appropri
More Less

Related notes for CMPT 225

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.