ECE 122. Engineering Problem Solving with Java

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

Introduction to Software Design

Chapter 13 Exception Handling

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

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Advanced Flow of Control -- Introduction

Exceptions. CSC207 Winter 2017

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

Errors and Exceptions

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

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

Internal Classes and Exceptions

14. Exception Handling

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

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

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

EXCEPTION HANDLING. Summer 2018

More on Exception Handling

Data Structures. 02 Exception Handling

Object Oriented Programming. Week 7 Part 1 Exceptions

More on Exception Handling

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

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

Chapter 12 Exception Handling

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

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

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

Programming II (CS300)

BBM 102 Introduction to Programming II Spring Exceptions

Object Oriented Programming

Exceptions - Example. Exceptions - Example

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

BBM 102 Introduction to Programming II Spring 2017

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

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

Exception Handling. Exception Handling

Fundamentals of Object Oriented Programming

C16b: Exception Handling

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

Exception-Handling Overview

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

2.6 Error, exception and event handling

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

Programming II (CS300)

Principles of Software Construction: Objects, Design and Concurrency. Exceptions, scope, static data and methods, and Generics.

Exception Handling. Handling bad user input. Dr. Siobhán Drohan Maireád Meagher. Produced by:

ITI Introduction to Computing II

Defensive Programming. Ric Glassey

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

Correctness and Robustness

Java Errors and Exceptions. Because Murphy s Law never fails

ITI Introduction to Computing II

Object Oriented Programming Exception Handling

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

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

CSCI 261 Computer Science II

Exceptions Handling Errors using Exceptions

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

Programming Languages and Techniques (CIS120)

Object Oriented Programming

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

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

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

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

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

Chapter 12 Exception Handling and Text IO. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

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

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

6.Introducing Classes 9. Exceptions

Lecture 19 Programming Exceptions CSE11 Fall 2013

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

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

Exception Handling. Chapter 9

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

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

IS311 Programming Concepts 2/59. AVA Exception Handling Jการจ ดการส งผ ดปรกต

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

Chapter 14. Exception Handling and Event Handling

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

EXCEPTIONS. Java Programming

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions in Java

File I/O and Exceptions

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

CSC 1214: Object-Oriented Programming

Sri Vidya College of Engineering & Technology Question Bank

Here is a hierarchy of classes to deal with Input and Output streams.

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

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

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

CS 112 Programming 2. Lecture 08. Exception Handling & Text I/O (1) Chapter 12 Exception Handling and Text IO

Chapter 14. Exception Handling and Event Handling ISBN

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Chapter 3 Java Exception

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

Java Loose Ends. 11 December 2017 OSU CSE 1

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

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions

Overview Problem: Can we detect run-time errors and take corrective action? Try-catch Test for a variety of different program situations Java provides a series of test cases Allows for graceful exit of program or treatment of special cases

Errors Errors do occur in programming. Problems opening a file, dividing by zero, accessing an out-of-bounds array element, hardware errors, and many more The question becomes: What do we do when an error occurs? How is the error handled? Who handles it? Should the program terminate? Can the program recover from the error? Should it? Java uses exceptions to provide the error-handling capabilities for its programs.

Exceptions An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Represented as an object in Java Throwing an exception An error occurs within a method. An exception object is created and handed off to the runtime system. The runtime system must find the code to handle the error. Catching an exception The system searches for code to handle the thrown exception. It can be in the same method or in some method in the call stack.

Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program 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 Usually represents a unrecoverable situation and should not be caught

Handling Exceptions General form: try { statement(s); } catch (ExceptionType name) { statement(s); } finally { statement(s); }

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

Exception Class Hierarchy Java has a predefined set of exceptions and errors that can occur during execution. Exception (derived from Throwable) RunTimeException ClassNotFoundException IOException ArithmeticException NullPointerException IndexOutOfBoundsException This is just a small part of the Exception hierarchy NumberFormatException

The try Statement Handling 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 exception occurs: Processing continues at the first catch clause that matches the exception type

Handling Exceptions Three statements help define how exceptions are handled: try- identifies a block of statements within which an exception might be thrown catch - must be associated with a try statement and identifies a block of statements that can handle a particular type of exception. - The statements are executed if an exception of a particular type occurs within the try block. - A try statement can have multiple catch statements associated with it. finally - must be associated with a try statement - identifies a block of statements that are executed regardless of whether or not an error occurs within the try block. - Even if the try and catch block have a return statement in them, finally will still run.

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 If an exception is generated: The statements in the finally clause are executed after the statements in the appropriate catch clause complete

Summary Exceptions form an important part of Java Graceful error checking and handling allow for better designed code Try-catch evaluates a number of possibilities Often error messages are the result