Download presentation
Presentation is loading. Please wait.
1
Chapter 8 Errors and Exceptions
2
Overview Handling errors Preventing resource leaks.
With return values. With setjmp/longjmp. With exceptions. Preventing resource leaks. Logging and debugging.
3
Handling errors with return codes.
Simple idea but: Makes it easy to ignore errors The code becomes harder to read, write and understand. There is no universal convention for communicating error information.
5
An example of error checking
6
Useful C Functions for handling errors
errno perror strerror error and friends err/warn setjmp/longjmp
7
Exceptions in C++ Mechanism:
try{....}catch(....){...}catch(...){...}... First sequence of code is code to execute. This code can “throw” exceptions which are “caught” by the apprpriate catch phrase.
8
Managing exceptions Exceptions in java vs C++ (checked or unchecked)
Not catching an exception will cause a program crash, -> DOS attack. At least, catch everything at top level to avoid “spilling beans”. Deeper down, catch only what you can handle. Watch for “finally” clauses in Microsoft C++/Java (pp 273/274)
9
Preventing Resource Leaks
Only real security risk is DOS, but can cause serious performance problems. Very hard to track down and identify. Usually manifest themselves only in production Very hard to trace back to their origin. Usually due to seldom traversed instruction paths, like error or exception handlers.
10
Watch out for multiple returns
14
Logging and Debugging Centralize the output operation. There are packages for this. Provide a uniform view. Make it easier to change medium, machine, etc. Provide time stamps. Log every important action, including failures! Protect the logs.
15
A few final words Keep debugging aids out of production
Keep back-door access code out of production Clean out Backup files Say “NO” to Easter Eggs.
16
Resource leaks “gotchas”
Watch for multiple return statements In C++, classes can be used to advantage, but watch out for strange modifications.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.