Exception Handling. Exception Handling

Similar documents
ECE 122. Engineering Problem Solving with Java

Lecture 19 Programming Exceptions CSE11 Fall 2013

CS159. Nathan Sprague

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

ITI Introduction to Computing II

C16b: Exception Handling

ITI Introduction to Computing II

For more details on SUN Certifications, visit

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

CS159. Nathan Sprague

More on Exception Handling

Fundamentals of Object Oriented Programming

Programming II (CS300)

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

More on Exception Handling

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

Chapter 13 Exception Handling

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

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

BBM 102 Introduction to Programming II Spring Exceptions

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

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

Exception Handling CSCI 201 Principles of Software Development

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

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

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

Exception-Handling Overview

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

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Introduction Unit 4: Input, output and exceptions

Correctness and Robustness

EXCEPTION HANDLING. Summer 2018

BBM 102 Introduction to Programming II Spring 2017

CSE 143 Java. Exceptions 1/25/

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

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

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

Java Errors and Exceptions. Because Murphy s Law never fails

Object Oriented Programming

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

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

Programming II (CS300)

Exceptions Handling Errors using Exceptions

COMP-202 Unit 9: Exceptions

COMP-202 Unit 9: Exceptions

Exceptions. CSC207 Winter 2017

Sri Vidya College of Engineering & Technology Question Bank

Data Structures. 02 Exception Handling

Recitation 3. 2D Arrays, Exceptions

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

Exceptions - Example. Exceptions - Example

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

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

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

CSC 1214: Object-Oriented Programming

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Chapter 12 Exception Handling

CSCI 261 Computer Science II

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

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

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

CS 3 Introduction to Software Engineering. 3: Exceptions

16-Dec-10. Consider the following method:

EXCEPTIONS. Java Programming

2.6 Error, exception and event handling

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

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

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

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

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

Software Practice 1 - Error Handling

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

Errors and Exceptions

Exception Handling Advanced Programming Techniques

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

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

Introductory Programming Exceptions and I/O: sections

CS 11 java track: lecture 3

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

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

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

17. Handling Runtime Problems

Internal Classes and Exceptions

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

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

Introduction to Software Design

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors 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

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 & logging Best Practices. Angelin


Checked and Unchecked Exceptions in Java

Exception Handling in Java

What are Exceptions?

Full file at Chapter 2 - Inheritance and Exception Handling

Programming Languages and Techniques (CIS120)

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

Input-Output and Exception Handling

Transcription:

References: Jacquie Barker, Beginning Java Objects ; Rick Mercer, Computing Fundamentals With Java; Wirfs - Brock et. al., Martin Fowler, OOPSLA 99 Tutorial ; internet notes; notes:h. Conrad Cunningham 11/13/2003 IT350 1 Exceptions are error conditions -- not bugs a bug is a defect in the code an error condition is a predictable failure that occurs under certain circumstances Exceptions are the way a program can handle gracefully an error that would cause a crash. 11/13/2003 IT350 2 The Java language provides many exception handlers for programmer errors Java runtime system detects an exceptional event an Exception object is constructed containing info about exception type runtime system throws an exception runtime system looks for code to handle the exception if no handler found, program terminates 11/13/2003 IT350 3 1

Example: Java throws an exception when an invalid number is entered public class HandleException public static void main ( String[] args ) TextReader keyboard = new TextReader(); System.out.println ( Enter a number: ); String numberstring = keyboard.readword(); double number = Double.parseDouble ( numberstring ); System.out.println( numberstring + stored in number as + number; ); 11/13/2003 IT350 4 Dialogue when number entered is not valid: Enter a number: 35o Exception in thread main java.lang.numberformatexception: 35o atjava.lang.floatingdecimal.readjavaformatstring (FloatingDecimal.java:1176) at java.lang.double.parsedouble(double.java.184) at HandleException.main(HandleException.java.io) Java displays a stack of method calls; main is the bottom of the stack the exception occurred in the method on the top 11/13/2003 IT350 5 We don t usually want the program to terminate, but rather to proceed with some alternative action in the event of an exception. So we write code to catch the exception A try block is wrapped around the code being executed. It is followed immediately by a catch block, which contains code that executes in the event of an error. We also specify in the catch block what type of exception is being thrown 11/13/2003 IT350 6 2

Using try/catch to Handle Exceptions public class HandleException public static void main ( String[] args ) TextReader keyboard = new TextReader(); System.out.println ( Enter a number: ); String numberasstring = keyboard. readword (); double number; try number = Double. parsedouble( numberasstring ); catch (NumberFormatException nfe) System.out. println(numberasstring + is not a valid number ); System.out. println( Setting number to -1.0 ); number = -1.0; System.out.println( numberasstring + stored in number as + number; ); 11/13/2003 IT350 7 Runtime Exceptions You must know when a method might throw an exception and what type of exception it throws. Read the documentation to find out! /** From Double class: * Return a floating-point number represented by the String * argument; If numberasstring does not represent a valid * number, this method will throw a number format * exception. */ public static double parsedouble(string numberasstring) throws NumberFormatException 11/13/2003 IT350 8 Handling an I/O Exception If an I/O connection fails, Java throws an IOException. This is the method for a URL connection: public URLConnection() throws IOException Here s the usage: java.net.url url = new java.net.url( http://www.sice.umkc.edu/ ) java.net.urlconnection conn; try conn= url.openconnection(); catch (java.io.ioexception e ) //an error has occurred; log an error, print a message or //throw another exception 11/13/2003 IT350 9 3

Unchecked Exceptions The parsedouble method does not catch the exception!! If the programmer does not put the method call in a try block and explicitly catch the exception, the program simply terminates (as shown in earlier example). This is one of the unchecked exceptions: one that doesn t need to be handled many unchecked exception classes extend the RuntimeException class. 11/13/2003 IT350 10 Handling exceptions in extending classes is optional Exceptions Classes Hierarchy Object Throwable Exception RuntimeException ClassNotFoundException IOException ArithmeticException FileNotFoundException...EOFException... NumberFormatException ArrayIndexOutOfBoundsException NullPointerException... 11/13/2003 IT350 11 Writing a Method That Throws an Exception public void deposit (double anamount) throws IllegalArgumentException if (anamount <= 0.0) throw new IllegalArgumentException(); balance = balance + anamount; The keyword throw must be followed by an instance of a class that extends Throwable. Because Exception extends Throwable, an instance of any Exception class can be thrown. 11/13/2003 IT350 12 4

Multiple Catch Statements There may be multiple categories of exceptions that you want to catch Each category is a separate catch block try //some code catch (NullPointerException e1) catch (IllegalArgumentException e2) catch (Exception e3) //This is a catch -all -- catches any type of exception that can be thrown finally //this code is executed even if an exception did not occur //commonly used to clean up internal state (like closing a file) 11/13/2003 IT350 13 5