Exceptions (part 2) An exception is an object that describes an unusual or erroneous situation. Quick Review of Last Lecture.

Similar documents
Introduction to Software Design

ECE 122. Engineering Problem Solving with Java

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Advanced Flow of Control -- Introduction

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

COMP-202. Exceptions. COMP Exceptions, 2011 Jörg Kienzle and others

Exceptions. Outline 7/31/2012. Exceptions. Exception handling is an important aspect of objectoriented. Chapter 10 focuses on:

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Exceptions. COMP 202 Exceptions. Exceptions. Exceptions. An exception is an object that describes an unusual or erroneous situation

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

File I/O and Exceptions

Object Oriented Programming

Internal Classes and Exceptions

Object Oriented Programming

14. Exception Handling

More on Exception Handling

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

More on Exception Handling

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

CS 3 Introduction to Software Engineering. 3: Exceptions

Chapter 14. Exception Handling and Event Handling ISBN

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Chapter 13 Exception Handling

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

CS Programming I: Exceptions

Exceptions in Java

CS Programming I: Exceptions

Exceptions Handling Errors using Exceptions

Exception Handling. Chapter 11. Java By Abstraction Chapter 11. Outline What Are Exceptions?

Program Correctness and Efficiency. Chapter 2

CSC 1214: Object-Oriented Programming

Correctness and Robustness

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

Java Loose Ends. 11 December 2017 OSU CSE 1

BBM 102 Introduction to Programming II Spring 2017

Research on the Novel and Efficient Mechanism of Exception Handling Techniques for Java. Xiaoqing Lv 1 1 huihua College Of Hebei Normal University,

Exceptions. Why Exception Handling?

BBM 102 Introduction to Programming II Spring Exceptions

Exceptions. CSC207 Winter 2017

Programming II (CS300)

Exceptions - Example. Exceptions - Example

Data Structures. 02 Exception Handling

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Object Oriented Programming. Week 7 Part 1 Exceptions

National University. Faculty of Computer Since and Technology Object Oriented Programming

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

Chapter 5: Enhancing Classes

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 14. Exception Handling and Event Handling

Sri Vidya College of Engineering & Technology Question Bank

Fundamentals of Object Oriented Programming

COSC Exception Handling. Yves Lespérance. Lecture Notes Week 10 Exception Handling

Comp 249 Programming Methodology Chapter 9 Exception Handling

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling

COMP1008 Exceptions. Runtime Error

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

6.Introducing Classes 9. Exceptions

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 14. Exception Handling and Event Handling 异常处理和事件处理. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Exception-Handling Overview

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

Designing Robust Classes

Lecture 19 Programming Exceptions CSE11 Fall 2013

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

C16b: Exception Handling

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Download link: Java Exception Handling

EXCEPTION HANDLING. Summer 2018

Exceptions. Examples of code which shows the syntax and all that

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Programming II (CS300)

Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

Exceptions. What exceptional things might our programs run in to?

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. Exception Handling

Exception Handling. Chapter 9

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Errors and Exceptions

Exceptions. Gunnar Gotshalks EX 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

ASSERTIONS AND LOGGING

CSCI 261 Computer Science II

Contents. I Introductory Examples. Topic 06 -Exception Handling

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Transcription:

(part 2) December 3, 2007 Quick Review of Last Lecture ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev An exception is an object that describes an unusual or erroneous situation. are thrown by a program, and may be caught and handled by another part of the program A program can be separated into a normal execution flow and an exception execution flow An error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught Exception Handling Java has a predefined set of exceptions and errors that can occur during execution A program can deal with an exception in one of three ways: ignore it handle it where it occurs handle it an another place in the program The manner in which an exception is processed is an important design consideration Exception Handling If an exception is ignored by the program, the program will terminate abnormally and produce an appropriate message The message includes a call stack trace that: indicates the line on which the exception occurred shows the method call trail that lead to the attempted execution of the offending line See Zero.java (page 533) 1

The Exception Class Hierarchy Zero.java Zero_Caught.java Classes that define exceptions are related by inheritance, forming an exception class hierarchy All error and exception classes are descendents of the Throwable class A programmer can define an exception by extending the Exception class or one of its descendants The parent class used depends on how the new exception will be used Class Hierarchy java.lang.arithmeticexception On-line Java Documentation http://java.sun.com/j2se/1.5.0/docs/api/index.html The try Statement To handle an exception in a program, the line that throws the exception is executed within a try block A try block is followed by one or more catch clauses Each catch clause has an associated exception type and is called an exception handler When an exception occurs, processing continues at the first catch clause that matches the exception type 2

The finally Clause A try statement can have an optional clause following the catch clauses, designated by the reserved word finally The statements in the finally clause always are executed If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete OutOfBounds.java OutOfBounds_Caught.java If an exception is generated, the statements in the finally clause are executed after the statements in the appropriate catch clause complete java.lang.indexoutofboundsexception NullReference.java NullReference_Caught.java java.lang.arrayindexoutofboundsexception java.lang.nullpointerexception ClassCast.java ClassCast_Caught.java 3

Example: ProductCodes.java (page 536) java.lang.classcastexception Valid Codes TRV2475A5R-14 4 th 7 th pos = district number Chapter 10 Sections 10.4-10.6 10 th position == zone Zone R is banned in distric > 2000 Exception Propagation Exception Propagation An exception can be handled at a higher level if it is not appropriate to handle it where it occurs propagate up through the method calling hierarchy until they are caught and handled or until they reach the level of the main method See Propagation.java (page 539) See ExceptionScope.java (page 540) A try block that contains a call to a method in which an exception is thrown can be used to catch that exception 4

Checked An exception is either checked or unchecked A checked exception either must be caught by a method, or must be listed in the throws clause of any method that may throw or propagate it A throws clause is appended to the method header The compiler will issue an error if a checked exception is not caught or asserted in a throws clause Unchecked An unchecked exception does not require explicit handling, though it could be processed that way The only unchecked exceptions in Java are objects of type RuntimeException or any of its descendants Errors are similar to RuntimeException and its descendants in that: Errors should not be caught Errors do not require a throws clause THE END 5