CS 461
Subroutines and Control Abstraction Exception Handling
Yanling Wang
Computer Science and Engineering Penn State University
Carnegie Mellon
1
Outline
¢ This Lecture – Exception Handling
¢ This Lecture – Understanding Call Stack
Carnegie Mellon
2
EXCEPTION HANDLING
Carnegie Mellon
3
Exceptions
So far, we assumed each function will run from start to its return points
But a program also needs to handle exceptions: ¢ Out of memory
¢ Divide by zero
¢ File not found
¢…
Carnegie Mellon
4
4
Workaround in C language
¢ Return a special value (e.g., NULL) when a real value cannot be computed
¢ Return an explicit ”status” to indicate if an exception happens
¢ Rely on the caller to pass in the exception handling code (e.g., via function pointer)
Exception handling is not enforced (error-prone) Clutter up the program, especially for normal cases
Carnegie Mellon
5
5
Exception Handling
A language feature that
¢ Isolate error-checking code
¢ Direct execution to a handler when appropriate
Define exceptions?
How does exception change control flow?
Carnegie Mellon
6
6
Defining Exceptions Pre-defined exceptions
¢ e.g., divide by zero, out-of-bound array access In Object-Oriented Language
¢ Typically, defined as subclass from exception class (i.e., in C++, inherit from exception class)
Carnegie Mellon
class MyException:public exception {
virtual const char* what() const throw()
{
return “My exception happened”;
}
} myex;
7
7
Throwing Exceptions Pre-defined exceptions
¢ Automatically generated
User-defined exceptions
¢ Generated by “throw” keyword
Carnegie Mellon
int foo () {
…
throw myex;
…
}
8
8
Catching Exceptions
Carnegie Mellon
try {
.. code that might throw exceptions ..
}
catch (Exception1 e1) {.. handling code ..}
catch (Exception2 e2) {.. handling code ..}
…
9
Catching Exceptions – finally
Some languages (i.e. Java) uses finally to clean up resources.
Carnegie Mellon
try {
.. code that might throw exceptions ..
}
catch (Exception1 e1) {.. handling code ..}
catch (Exception2 e2) {.. handling code ..}
…
finally {.. this code always executes,
e.g. cleanup routine ..}
10
Catching Exceptions – RAII
Some languages (i.e. C++ and .Net languages such as C#)
• Resource Acquisition Is Initialization
• Clean up is done is object destructor
• Automatically called when object is going out of scope
Carnegie Mellon
try {
.. code that might throw exceptions ..
}
catch (Exception1 e1) {.. handling code ..}
catch (Exception2 e2) {.. handling code ..}
…
11
The “Replacement” Semantics
Carnegie Mellon
int foo () {
…
try { …
throw new myException(10);
… // this is replaced with handling code
} catch (myException e) {
.. Handling code …
}
… }
12
Exception Propagation
current frame
A
Carnegie Mellon
If no handler is found
If no handler is found
control flow continues after the handler in C
The exception mechanism needs to
B
C
D
properly follow calling sequences to Stack with Frames properly “exit” functions (e.g., restore
registers, destroy frame)
13
Exception Implementation When encounter an exception at pc:
¢ Go through handlers associated with pc in sequence
¢ Each handler either executes (when exception matches) or re-throws the exception for next handler
¢ If exception not handled, use a function-level handler: exit the function properly and re-throw the exception
¢ Now, pc is in the caller, and repeat the first bullet in the caller
Carnegie Mellon
14
UNDERSTANDING CALL STACK
Carnegie Mellon
15
Activation Record (Stack Frame)
¢ Local variables are allocated in current function instance’s AR
¢ Local variables not initialized will get values originally occupying the stack frame (from a previous function call).
¢ Pointers to local variables will become invalid (or pointing to things you don’t expect) after function returns.
Carnegie Mellon
16
stack1.c
Carnegie Mellon
void foo() {
int nums[2];
int i;
printf(“%d “, i++);
}
int *bar() {
int nums[2];
int i;
printf(“bar i before: %d\n”, i);
scanf(“%d”, &i);
printf(“bar i after: %d\n”, i);
return &i;
}
int main() {
int j;
int *ptr;
ptr = bar();
printf(“before foo: %d\n”, *ptr);
for (j = 1; j <= 10; j++) foo();
printf("\n");
printf("after foo: %d\n", *ptr);
return 0;
}
17