Chapter 11 Exception Handling

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

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

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

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

ECE 122. Engineering Problem Solving with Java

Programming II (CS300)

Fundamentals of Object Oriented Programming

Programming II (CS300)

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

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

CS112 Lecture: Exceptions and Assertions

Chapter 12 Exception Handling

Designing Robust Classes

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

ITI Introduction to Computing II

Correctness and Robustness

For more details on SUN Certifications, visit

Data Structures. 02 Exception Handling

ITI Introduction to Computing II

Std 12 Lesson-10 Exception Handling in Java ( 1

CSC Java Programming, Fall Java Data Types and Control Constructs

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

More on Exception Handling

More on Exception Handling

Object Oriented Programming

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

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

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

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

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

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

Chapter 9 Inheritance

Lab5. Wooseok Kim

Comp 249 Programming Methodology Chapter 9 Exception Handling

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

Vendor: Oracle. Exam Code: 1Z Exam Name: Java SE 8 Programmer. Version: Demo

Object Oriented Programming. Week 7 Part 1 Exceptions

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Chapter 5 Control Structures

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

Java Errors and Exceptions. Because Murphy s Law never fails

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

EXCEPTION HANDLING. Summer 2018

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Typed Lambda Calculus and Exception Handling

C16b: Exception Handling

Exception Handling Advanced Programming Techniques

WOSO Source Code (Java)

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

Binary search. int index = Collections.binarySearch(list, element);

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

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

SECTION-1 Q.1.Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

Exception handling & logging Best Practices. Angelin

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Exceptions. CSC207 Winter 2017

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

COE318 Lecture Notes Week 10 (Nov 7, 2011)

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

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

Object Oriented Programming Exception Handling

Full file at Chapter 2 - Inheritance and Exception Handling

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

Chapter 13 Exception Handling

Object Oriented Programming

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

Module 4 - 异常和断言 一 选择题 :

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Exceptions and Error Handling

Java Loose Ends. 11 December 2017 OSU CSE 1

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

Getting Started with Python

What are Exceptions?

16-Dec-10. Consider the following method:

Lara Technologies Special-Six Test

Input-Output and Exception Handling

Special Topics: Programming Languages

CSCI 261 Computer Science II

10/3/2018 Programming Data Structures. Casting: upcasting, downcasting Debugging again! Yay! Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

Java Exceptions Java, winter semester

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

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

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

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

Graphical User Interface (Part-3) Supplementary Material for CPSC 233

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

Exceptions Handling Errors using Exceptions

Exception Handling in Java

Chapter 14. Exception Handling and Event Handling

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

File I/O and Exceptions

CompSci 220. Programming Methodology 16: Understanding FP Error Handling Part 2

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Transcription:

Chapter 11 Exception Handling I. Scott MacKenzie 1 Outline Last slide on Mar 22 2 1

Runtime Errors // From Figure 11.1 in course text public class ExceptionHandling01 IO.println("Enter a string containing a slash"); IO.println("Left substring: " + left); IO.println("Right substring: " + right); ExceptionHandling01.java Demo 3 Extracting Substrings Exception 4 2

substring API The API of this method states that if certain conditions are not met, an exception of a specified type is thrown. This means the method does not handle the exception; it delegates that to the caller 5 try catch construct 6 3

Using the try catch Construct // From Figure 11.5 in course text public class ExceptionHandling02 IO.println("Enter a string containing a slash"); try IO.println("Left substring: " + left); IO.println("Right substring: " + right); catch (IndexOutOfBoundsException e) IO.println("No slash in input!"); //IO.println(e); IO.println("Exiting application. Have a nice day!"); ExceptionHandling02.java Demo 7 Multiple catch Construct 8 4

// From Figure 11.7 in course text Reading a Fraction public class ExceptionHandling03 IO.println("Enter a fraction"); int leftint = Integer.parseInt(left); int rightint = Integer.parseInt(right); int answer = leftint / rightint; IO.println("Quotient = " + answer); IO.println("Exiting application. Bye."); Note: Different inputs yield different exceptions. Try 123.456 123/abc 123/0 ExceptionHandling03.java Demo 9 Reading a Fraction (Robust Version) // From Figure 11.8 in course text public class ExceptionHandling04 try IO.println("Enter a fraction"); int leftint = Integer.parseInt(left); int rightint = Integer.parseInt(right); int answer = leftint / rightint; IO.println("Quotient = " + answer); catch (IndexOutOfBoundsException e) IO.println("No slash in input!"); catch (NumberFormatException e) IO.println("Non-integer operands"); catch (ArithmeticException e) IO.println("Cannot divide by zero!"); IO.println("Exiting application. Bye."); ExceptionHandling04.java Demo Last slide on Mar 24 Note: Different inputs yield different exceptions. Try 123.456 123/abc 123/0 10 5

Reading a Fraction (Friendly Version) // From Figure 11.8 in course text public class ExceptionHandling05 boolean finished = false; while (!finished) try IO.println("Enter a fraction"); int leftint = Integer.parseInt(left); int rightint = Integer.parseInt(right); int answer = leftint / rightint; IO.println("Quotient = " + answer); finished = true; catch (IndexOutOfBoundsException e) IO.println("No slash in input!"); catch (NumberFormatException e) IO.println("Non-integer operands"); catch (ArithmeticException e) ExceptionHandling05.java Demo Note: Different inputs yield different exceptions. Try 123.456 123/abc 123/0 11 Throwable Hieararchy 12 6

Without Exception Handling 13 Wrapped Exceptions The application throws a highly specialized exception called SQLException. To hide its details, the application creates an ordinary runtime exception and wraps the specialized exception in it as the cause. 14 7

Logic Errors Review: Three types of errors: Syntax (produce compile errors) Runtime (produce exceptions) Logic Logic errors are most troublesome because they produce no visible sign other than incorrect behaviour or results Logic errors can be converted to runtime errors by designing programs according to S/E principles; i.e., by asserting assumptions. In so doing, logic errors throw exceptions which can be caught and handled accordingly 15 Last slide on Mar 26 Thank You 16 8