1 / 16

Complete Guide: Handling Errors, Exceptions, and Resource Leaks

Learn to manage errors efficiently with return codes, setjmp/longjmp, exceptions, and prevent resource leaks. Explore useful functions and best practices for logging, debugging, and preventing security risks. Keep your code clean and robust.

Download Presentation

Complete Guide: Handling Errors, Exceptions, and Resource Leaks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Errors and Exceptions Chapter 8

  2. Overview • Handling errors • 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.

  4. An example of error checking

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

  6. 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.

  7. 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)‏

  8. 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.

  9. Watch out for multiple returns

  10. 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.

  11. 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.

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

More Related