Exceptions CSCE 121 J. Michael Moore

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Exception Handling. Introduction One benefit of C++ over C is its exception handling system. An exception is a situation in which a program has an unexpected.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Handling Errors with Exception (in Java) Project 10 CSC 420.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Exceptions COMPSCI 105 S Principles of Computer Science.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
1 CSC241: Object Oriented Programming Lecture No 27.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Exceptions Handling Exceptionally Sticky Problems.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Exception Handling How to handle the runtime errors.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Java Exceptions a quick review….
Exception Handling C++.
C++ Exceptions.
Chapter 16 Exception Handling
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Java Programming Fifth Edition
Handling Exceptionally Sticky Problems
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Chapter 5: Loops and Files.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Dynamic Memory CSCE 121 J. Michael Moore.
Object Oriented Programming COP3330 / CGS5409
Exceptions C++ Interlude 3
References and Pointers
Exception Handling Chapter 9.
Namespaces CSCE 121 J. Michael Moore
Anatomy of a Function Part 2
Exceptions with Functions
Chapter 17 Templates and Exceptions Part 2
Exception Handling.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Exception Handling Oo28.
Exceptions and Templates
Part B – Structured Exception Handling
Input Validation CSCE 121 J. Michael Moore
Exceptions 1 CMSC 202.
Standard Version of Starting Out with C++, 4th Edition
Object-Oriented Programming (OOP) Lecture No. 43
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Handling Exceptionally Sticky Problems
Lecture 9.
Input Validation CSCE 121 Based on slides created by Carlos Soto.
Chapter 11: Exception Handling
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Lecture 3 More on Flow Control, More on Functions,
CMSC 202 Lesson 20 Exceptions 1.
IST256 : Applications Programming for Information Systems
Presentation transcript:

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

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; }

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…

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

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.

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

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.

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

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.

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

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.

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

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)

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.