Lecture13-Eception_Handling

Lecture13-Eception_Handling - Computer Programming II 1...

Info iconThis preview shows pages 1–8. 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: Computer Programming II 1 Lecture 13 Exception Handling Computer Programming II 2 Learning Objectives To undersrand what is exception To understand exception handling through the use of try, throw and catch block To understand how exception propagation works To understand how to wrote multiple catch blocks and exception Matching To realize the advantages of exception handling Computer Programming II 3 Exception When a program is executed, unexpected situation may occur. Such a situation is called an exception In other word: Exception is a runtime error caused by some abnormal conditions Example: division by zero failure of new operator to obtain a requested amount of memory Exception handler is code that handles the exception (runtime error) when it occurs Computer Programming II 4 Exception Example: Division By Zero double divide (double x, double y) { return x / y; // divide by 0 if y = 0 } int main() { double x, y; cin >> x >> y; cout << "Result = " << divide (x, y); } How to deal with the error below? Computer Programming II 5 Exception Example: Division By Zero double divide (double x, double y) { return x / y; // divide by 0 if y = 0 } int main() { double x, y; cin >> x >> y; if (y == 0) cout << "Cannot divide by zero\n"; else cout << "Result = " << divide (x, y); } A solution is shown below. It works but the codes that handles the error mixes with the codes for division, making the codes harder to read ( if for division, else for error handling, or the other way? No direct indication from if/else keywords alone. Computer Programming II 6 C++ implements exception handling using try , throw and catch block try block:-Write the code that might generate runtime error within the try block. Format: try { // Code that may generate // exceptions } Exception Handling Computer Programming II 7 throw statement:-Use keyword throw in try block to signal that abnormal condition or error has occurred ....
View Full Document

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 20

Lecture13-Eception_Handling - Computer Programming II 1...

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

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