Chapter 8 Errors and Exceptions Overview Handling errors

  • Slides: 16
Download presentation
Chapter 8 Errors and Exceptions

Chapter 8 Errors and Exceptions

Overview Handling errors With return values. With setjmp/longjmp. With exceptions. Preventing resource leaks. Logging

Overview Handling errors With return values. With setjmp/longjmp. With exceptions. Preventing resource leaks. Logging and debugging.

Handling errors with return codes. Simple idea but: Makes it easy to ignore errors

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.

An example of error checking

An example of error checking

Useful C Functions for handling errors errno perror strerror and friends err/warn setjmp/longjmp

Useful C Functions for handling errors errno perror strerror and friends err/warn setjmp/longjmp

Exceptions in C++ Mechanism: try{. . }catch(. . ){. . . }catch(. . .

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.

Managing exceptions Exceptions in java vs C++ (checked or unchecked) Not catching an exception

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)

Preventing Resource Leaks Only real security risk is DOS, but can cause serious performance

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.

Watch out for multiple returns

Watch out for multiple returns

Logging and Debugging Centralize the output operation. There are packages for this. Provide a

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.

A few final words Keep debugging aids out of production Keep back-door access code

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.

Resource leaks “gotchas” Watch for multiple return statements In C++, classes can be used

Resource leaks “gotchas” Watch for multiple return statements In C++, classes can be used to advantage, but watch out for strange modifications.