Exceptions Handling Exceptionally Sticky Problems.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Error-handling using exceptions
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
COMP 121 Week 5: Exceptions and Exception Handling.
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.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Foundations of Program Design Sixth Edition
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
CIS 270—Application Development II Chapter 13—Exception Handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Chapter 8-Exception Handling/ Robust Programming.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Introduction to Exceptions in Java CS201, SW Development Methods.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Handling Exceptionally Sticky Problems
MIT AITI 2003 Lecture14 Exceptions
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Throwing and catching exceptions
Part B – Structured Exception Handling
Handling Exceptionally Sticky Problems
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Exceptions.
Presentation transcript:

Exceptions Handling Exceptionally Sticky Problems

Handling Errors Some things can't be done

Guarding Can protect against foreseeable errors: – ALWAYS validate user input

Lack Of Context Low level code – How should it respond?

Lack Of Context What should we do with a bad zip code?

Proper Response Proper response to low level errors depends on high level code – Is this a GUI app? – Unattended server application?

Option 1 - Assert assert( expression ) If expression is not true, self-destruct with message

Option 2 - Return Devise special error code to return

Option 2 - Return Not always an option… – Any possible int might be a valid return value

Extra parameters Can add extra parameters for error code… Yuck

Option 3 - Exceptions Exceptions : alternative return mechanism – Way for code to return an error Indicate error by throwing a value: – Does not have to match return type

Catching A thrown exception will blow up your program…

Catching …unless you catch it try : Try this code… something bad might happen catch : Here is how to handle any exceptions – Only run if an exception thrown in try

Catch Catch specifies – Type of thing it catches Only catches that type – What it will call the thing it caught Use as variable inside catch

Catch Wrong Type == No Catch Can have multiple catches: catch(…) Catches anything - But can't use it as variable

Defining Exceptions Exception can be anything – Custom type:

Stack Unwinding Call stack stores how we got to a line of code:

Stack Unwinding Thrown exception works back down stack looking for a catch – Does function C catch? – No, how bout B? – No, how bout A? – No, how bout Main?

Std::Excptions Std library defines exception class exception is parent to all – Has virtual what() function

Exception Subclasses Many subclasses to exception – All support what() – All can be caught as exception

Exception Subclasses Can't add information to plain exception: Sub classes – Allow for construction with string message – Help specify problem

Proper Catching Using out_of_range exception class:

Reacting to Exceptions Choices for dealing with exceptions – Fix the error – Log & Continue – Blow up But get to make decision at appropriate level

Announcing a Throw Can announce what your function throws: – I throw nothing: – I thrown exception (or subtypes) – I throw these two types

What programmers want Programmers think this means: – Announce to other programmers what to expect – Check at compile time that someone will catch – Speed things up by not worrying about other types

What compiler does Compiler does: – Check at compile time that someone will catch Blow up program at run time if it throws something else – Speed things up by not worrying about other types Probably slow things down by doing checks at runtime

What Programmers Need Use comments to announce what to expect: – Announce to other programmers what to expect – Check at compile time that someone will catch – Speed things up by not worrying about other types

Final Thoughts When to throw – Exceptional problem you can't handle at this level – Still try to prevent errors at higher level What to throw – Approproiate std exception (i.e. out_of_range) – std::logic_error : good catch all