Exceptions - Example. Exceptions - Example

Similar documents
Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

Intro to Computer Science II. Exceptions

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

BBM 102 Introduction to Programming II Spring Exceptions

Exceptions. CSC207 Winter 2017

ITI Introduction to Computing II

Errors and Exceptions

ITI Introduction to Computing II

ECE 122. Engineering Problem Solving with Java

BBM 102 Introduction to Programming II Spring 2017

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

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

Input-Output and Exception Handling

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

11/1/2011. Chapter Goals

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

CS159. Nathan Sprague

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

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

Introductory Programming Exceptions and I/O: sections

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

Introduction to Software Design

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

Lecture 4. Types, Memory, Exceptions

CSCI 261 Computer Science II

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

EXCEPTION HANDLING. Summer 2018

More on Exception Handling

Exceptions Handling Errors using Exceptions

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

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

CS159. Nathan Sprague

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.

More on Exception Handling

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

Projeto de Software / Programação 3 Tratamento de Exceções. Baldoino Fonseca/Márcio Ribeiro

Introduction to Computer Science II CS S-22 Exceptions

2.6 Error, exception and event handling

17. Handling Runtime Problems

Full file at Chapter 2 - Inheritance and Exception Handling

Exception-Handling Overview

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

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

Programming Languages and Techniques (CIS120)

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

9. Java Errors and Exceptions

C16b: Exception Handling

Chapter 13 Exception Handling

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

Main concepts to be covered. Handling errors. Some causes of error situations. Not always programmer error. Defensive programming.

Programming II (CS300)

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

Object Oriented Programming Exception Handling

Exceptions and Libraries

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

Exception Handling CSCI 201 Principles of Software Development

Software Practice 1 - Error Handling

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

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

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

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. Java Programming

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

Download link: Java Exception Handling

CS112 Lecture: Exceptions and Assertions

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

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

CSPP : Introduction to Object-Oriented Programming

What are Exceptions?

Fundamentos de programação

Comp 249 Programming Methodology Chapter 9 Exception Handling

Chapter 11 Handling Exceptions and Events. Chapter Objectives

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

Exceptions in Java

6.Introducing Classes 9. Exceptions

CSE 331 Software Design & Implementation

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Programming II (CS300)

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

COMP1008 Exceptions. Runtime Error

Java Programming Unit 7. Error Handling. Excep8ons.

Exceptions and Error Handling

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

CS193j, Stanford Handout #25. Exceptions

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

CS 3 Introduction to Software Engineering. 3: Exceptions

CSE 143 Java. Exceptions 1/25/

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

What can go wrong in a Java program while running?

Defensive Programming. Ric Glassey

7. Java Input/Output. User Input/Console Output, File Input and Output (I/O)

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

Contents. I Introductory Examples. Topic 06 -Exception Handling

COS226 - Spring 2018 Class Meeting # 21 April 23, 2018

Software Practice 1 - Error Handling Exception Exception Hierarchy Catching Exception Userdefined Exception Practice#5

Transcription:

- Example //precondition: x >= 0 public void sqrt(double x) double root; if (x < 0.0) //What to do? else //compute the square root of x return root; 1 - Example //precondition: x >= 0 public void sqrt(double x) double root; if (x < 0.0) //throw an error exception throw new IllegalArgumentException( square is lower than 0 ); else //compute the square root of x return root; 2 1

This programm causes an error: public class makeexception static public void main (String[] args) int a = 100; int b = 0; int c = 0; c = a/b; Exception in thread "main java.lang.arithmeticexception: / by zero at MakeException.main(MakeException.java:15) 3 Class hierarchy Throwable unchecked checked Error Exception checked MyException unchecked Runtime Exception 4 2

Class Throwable: public String getmessage() yields an error message. public void printstacktrace() yields the function stack that is the calling hierarchie. 5 6 3

Checked and unchecked exceptions: A checked exception indicates an error case which you cannot avoid in each case. Sometimes it will occur. All subclasses of IOException are checked. In contrast to that, an unchecked exception indicates an error which you can and should avoid in any case. All subclasses of RuntimeException, for example IllegalArgumentException, NullPointerException are unchecked. When you call a method that throws a checked exception, the compiler checks that you do not ignore it. You must tell the compiler what you are going to do. In contrast to that, the compiler does not require you to keep track of unchecked exceptions. Error: A class for really bad errors, the program must terminate (for example OutOfMemoryError). 7 public void readfile(string filename) throws FileNotFoundException 8 4

public void readfile(string filename) try FileReader reader = new FileReader(filename); Scanner in = new new Scanner(reader); catch(filenotfoundexception e) //here comes the error handling 9 public void readfile(string filename) throws FileNotFoundException, 10 5

Since FileNotFoundException is a subclass of IOException, we can also write: public void readfile(string filename) throws IOException 11 The compiler can be forced to track back unchecked exceptions: public void sqrt(double x) throws IllegalArgumentException 12 6

If an error takes place, a error message called exception is thrown. An error message is represented by the class java.lang.exception (subclass of Throwable) An exception indicates an error which possibly can be handled. Handling by the catch- throw- rule: Handle the exception (error) by the current block or give it back to the calling block. The user can write an own Exception class as a subclass of Exception. The Error class represent very wrong errors. The program has to be terminated. There are checked and unchecked exceptions. The compiler has to be told what to do about the exception if it is ever thrown. 13 This programm causes an error: Which one? public class makeexception static public void main (String[] args) int[ ] arr = new int[100]; int n; for (n=0;;) arr[n++] = 0; 14 7

try String filename = ; FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); String input = in.next(); int value = Integer.parseInt(input); catch(ioexception e) e.printstacktrace(); catch(numberformatexception e) System.out.println( Input was not a number ); 15 General try Block try //instructions which possibly causes an error... catch (ExceptionType1 e1) //handle the exception of type ExceptionType1... catch (ExceptionType2 e2)...... catch (ExceptionTypeN en)... finally //limit the damage of the error... 16 8

The finally Clause The code of the finally clause is executed whenever the try block is exited in any of three ways After completing the last statement of the try block After completing the last statement of a catch clause, if this try block caught an exception When an exception was thrown in the try block not caught an exception Use it to leave the try- and- catch in a regular mode. 17 Example: FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); readdata(in); reader.close(); //may never get here What to do when of the methods before the last line throws an exception? 18 9

Place the call to close inside a finally clause: FileReader reader = new FileReader(filename); try Scanner in = new Scanner(reader); readdata(in); finally reader.close(); 19 Quality tip: Do not use catch and finally in the same try statement! try FileReader reader = new FileReader(filename); try //Read input Scanner in = new Scanner(reader); readdata(in); finally reader.close(); catch (IOException e) //Handle exception 20 10

Exception Designing your own exception types: Typially as a subclass of RuntimeException. Example: class NewException extends RuntimeException public NewException(String message) super(message);... 21 11