Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions CSCE 121 J. Michael Moore

Similar presentations


Presentation on theme: "Exceptions CSCE 121 J. Michael Moore"— Presentation transcript:

1 Exceptions CSCE 121 J. Michael Moore
Based on slides created by Carlos Soto.

2 Handling runtime errors
Check for unexpected conditions cout << “Enter a month (1-12): ”; int month; cin >> month; if (month < 1 || month > 12) { cout << “Invalid month.” << endl; }

3 Handling runtime errors: If statements?
if (month < 0 || month > 12) { cout << “Invalid month.” << endl; } else { cout << “Enter a day (1-31): ”; int day; cin >> day; if (day < 1 || day > 31) { cout << “Invalid day.” << endl; cout << “Enter a year (1-2016): ”; //... Code cluttered with error handling! Lose track of what you are trying to accomplish! This can get out of hand…

4 Exceptions Rationale Prior to exceptions
Code became cluttered with dealing with errors. Hard to focus on the underlying problem.

5 Goal Write code in a general way to solve the problem.
Add structure to deal with error that does not obscure the code solving the problem.

6 Exceptions: Throw, Try and Catch
When an unexpected condition happens, you “throw” an exception The Try block is the part of your code where an exception might occur You “catch” exceptions and deal with them in an exception handler

7 Exceptions: Throw, Try and Catch
When an unexpected condition happens, “throw” an exception We’ll talk about how to throw an exception later where it makes sense. (i.e. after we talk about functions) Generally only within a function we write.

8 Exceptions: Throw, Try and Catch
The Try block is the part of your code where an exception might occur

9 Handling runtime errors
Go ahead and use without worrying about validity. What happens if month’s value is invalid? try { cout << “Enter a month (1-12): ”; int month; cin >> month; int month6 = halfYear(month); cout << “6 months after ” << month << “ is “ << month6; } Exception can happen here.

10 Exceptions: Try, Throw and Catch
You “catch” exceptions in an exception handler (aka catch block) This is like a function that takes the exception as an argument

11 Handling runtime errors
try { cout << “Enter a month (1-12): ”; int month; cin >> month; int month6 = halfYear(month); cout << “6 months after ” << month << “ is “ << month6; } catch (int exception) { cout << “Invalid month.” << endl; Normal Case. Exceptional Case handled in a separate block.

12 Notes on Exceptions We can have multiple separate catch blocks (aka exception handlers) for multiple datatypes Allows us keep track of different types of errors

13 Handling runtime errors
catch (int exception) { if (exception == 1) { cout << “Error 1” << endl; } catch (char exception) { if (exception == ‘a’) { cout << “Error 2” << endl; The catch block only runs if the try block throws an exception. Then after it finishes, the program continues execution after the catch block. It takes an exception variable as an argument. We’ll see more about that when we cover functions. Let’s see what that looks like for more than one exception (go to code)

14 More Later Basics here More useful with functions
We’ll revisit this topic later. In reality, we never throw something in the same function where the try block is. For now, use try and catch. Later we’ll throw exceptions.


Download ppt "Exceptions CSCE 121 J. Michael Moore"

Similar presentations


Ads by Google