supp-exception

supp-exception - CSC 1111 Introduction to Computing using...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Exception Handling
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Introduction Exceptions – Run-time errors Exception Handling – A widely used method to handle run-time errors
Background image of page 2
3 How Exception Handling Was Done Before Within a function/method, use a variable to keep track of success or failure of an operation Between functions/methods, use the return value to indicate success or failure. Not appropriate for functions/methods that need to return a value (i.e., error codes may conflict with the return values) e.g.: a function that converts a string to integer atoi("0"); // Returns 0 atoi("ABC"); // Also returns 0!
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 How Exception Handling Was Done Before Use a global variable or data member to serve as an error flag and have other functions examine it Have to check the error flag immediately after every function/method call Have to explicitly clear the error flag after the error is handled e.g.: istream and ostream classes After every I/O operation, we need to check if the EOF, fail, or bad flags have been set.
Background image of page 4
5 Other shortcomings of "Traditional" methods for handling runtime errors Mixing logic and error handling codes can make program difficult to read/debug Where should the errors be handled? e.g.: Function foo() fails to open a file for input. Should foo() handle the error or should foo() let the caller handles the error? Perform task1 If the preceding task did not execute correctly Perform error processing Perform task2 If the preceding task did not execute correctly Perform error processing
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Exception Handling Steps Raise or throw an exception when an error occurs Catch the thrown exception Declare what types of exception to catch
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

supp-exception - CSC 1111 Introduction to Computing using...

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

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