Handling Exceptionally Sticky Problems

Slides:



Advertisements
Similar presentations
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Advertisements

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.
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.
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.
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.
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.
Exceptions Handling Exceptionally Sticky Problems.
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 –
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.
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.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
Eighth Lecture Exception Handling in Java
16 Exception Handling.
Exceptions.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Logger, Assert and Invariants
Handling Exceptionally Sticky Problems
MIT AITI 2003 Lecture14 Exceptions
Java Programming Language
Coding Defensively Coding Defensively
CS102 – Exceptions David Davenport Latest: May 2015
Indexer AKEEL AHMED.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling Chapter 9.
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 Chapter 9 Edited by JJ.
Throwing and catching exceptions
Exceptions CSCE 121 J. Michael Moore
Part B – Structured Exception Handling
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Exceptions.
Defensive Programming
Presentation transcript:

Handling Exceptionally Sticky Problems 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… Nothing forces client code to deal with it

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 http://www.cplusplus.com/reference/exception/exception/

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

Exception Subclasses Can't add information to plain exception: 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 What 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