Lecture-16-ErrorHandlingAndDefensiveProgramming

Lecture-16-ErrorHandlingAndDefensiveProgramming - Error...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Error Handling & Defensive Programming Error Handling Concepts Error conditions will occur: Out of memory, disk full, file missing, file corrupted, network error, Programs should anticipate and appropriately handle possible error conditions Software should be tested to see how it performs under various error conditions Simulate errors and see what happens Error Handling Concepts What should a program do when an error occurs? Some errors are "recoverable" - the program is able to recover and continue normal operation Many errors are "unrecoverable" - the program cannot continue and gracefully terminates Most errors are detected by low-level routines that are deeply nested in the call stack Low-level routines usually can't determine how the program should respond Information about the error must be passed up the call stack to higher-level routines that can determine the appropriate response Propagating Error Information Result codes Result object Error State Exceptions Result Codes A method uses its return value to tell the caller whether or not it succeeded In case of failure, the particular value returned can be used to determine the nature of the error int MyClass::OpenFile(string fileName) { } MyClass obj; int result = obj.OpenFile("index.html"); if (result < 0) { switch (result) { case -1: // file doesn't exist case -2: // file isn't writable case -3: // max number of files already open } } Result Codes Disadvantages of result codes You have to use the return value to return error info even if you'd rather use it to return something else Every time you call a method, you need to write code to check the return value for errors All of the error-checking code obscures the main flow of the program It's easy to write code that simply ignores errors because nothing forces you to check return values Result Object A method returns a Result object that contains A result code A message string The real return value of the method template <typename ReturnType> class Result { private: bool _status; string _message; ReturnType _value; public: Result(bool status, string message, ReturnType value) : _status(status), _message(message), _value(value) { return; } bool GetStatus() { return _status; } string GetMessage() { return _message; } ReturnType GetValue() { return _value; } }; Result Object Result<string> MyClass::GenerateSecurePassword() { } MyClass obj; Result<string> result = obj. GenerateSecurePassword(); if (result.GetStatus() == false) { cout << result.GetMessage() << endl; } else { string password = result.GetValue(); . . ....
View Full Document

Page1 / 33

Lecture-16-ErrorHandlingAndDefensiveProgramming - Error...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online