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

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

Errors and Exceptions

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

Writing your own Exceptions. How to extend Exception

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

BBM 102 Introduction to Programming II Spring Exceptions

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

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

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

BBM 102 Introduction to Programming II Spring 2017

Programming II (CS300)

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

Programming II (CS300)

16-Dec-10. Consider the following method:

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

Defensive Programming. Ric Glassey

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

Java Programming Unit 7. Error Handling. Excep8ons.

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

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

Chapter 13 Exception Handling

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

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

Exceptions in Java

Exceptions Handling Errors using Exceptions

CS159. Nathan Sprague

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

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

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

6.Introducing Classes 9. Exceptions

Exceptions - Example. Exceptions - Example

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

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

Introductory Programming Exceptions and I/O: sections

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

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

Exception-Handling Overview

Exceptions and Error Handling

Exceptions. CSC207 Winter 2017

COMP1008 Exceptions. Runtime Error

Sri Vidya College of Engineering & Technology Question Bank

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

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

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

Exceptions & Miscellaneous Lecture 17

13: Error Handling with Exceptions. The basic philosophy of Java is that "badly formed code will not be run."

EXCEPTIONS. Prof. Chris Jermaine

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

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

ECE 122. Engineering Problem Solving with Java

CS 3 Introduction to Software Engineering. 3: Exceptions

Data Structures. 02 Exception Handling

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

Comp 249 Programming Methodology Chapter 9 Exception Handling

Chapter 12 Exception Handling

Lecture 19 Programming Exceptions CSE11 Fall 2013

Video 2.1. Arvind Bhusnurmath. Property of Penn Engineering, Arvind Bhusnurmath. SD1x-2 1

Checked and Unchecked Exceptions in Java

EXCEPTION HANDLING. Summer 2018

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

CMSC 202. Exceptions

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

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

More on Exception Handling

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

Exceptions and Design

More on Exception Handling

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

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

Exceptions and Libraries

CS 251 Intermediate Programming Exceptions

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


CSE 331 Software Design & Implementation

Assertions and Exceptions Lecture 11 Fall 2005

Internal Classes and Exceptions

Introduction to Software Design

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

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

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

Motivations. Chapter 12 Exceptions and File Input/Output

Input-Output and Exception Handling

Exception Handling. Chapter 9

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

ITI Introduction to Computing II

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

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

CSCI 261 Computer Science II

Object Oriented Programming

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

ITI Introduction to Computing II

More on Objects in JAVA TM

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Full file at Chapter 2 - Inheritance and Exception Handling

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

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

CS159. Nathan Sprague

School of Informatics, University of Edinburgh

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

Transcription:

Exceptions Examples of code which shows the syntax and all that

When a method might cause a checked exception So the main difference between checked and unchecked exceptions was that the compiler forces us to handle or propagate a checked exception, while an unchecked exception simply propagates silently until it might be handled. What methods throw checked exceptions? For instance, methods which deal with I/O like file handling.

Opening a file for writing Two classes will help us write to a file: java.io.printwriter and java.io.filewriter. FileWriter can write data to a file (given in the constructor), which is fine. If we want to write text, PrintWriter is a good subclass, because it gives us convenient methods, such as println. To create a PrintWriter which prints to a file, you need to feed the constructor a FileWriter reference: PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt")); But what could go wrong here?

Things that might go wrong with files The important part of the previous slide s code was this: new FileWriter("OutFile.txt") We are assuming that we can create an object which can write to this file. What could go wrong? The file might exist but actually be a directory - not good ;-) The file cannot be created by us (filesystem/os says NO!) The file exists, but we cannot open it for some reason

Let s look at the constructor for FileWriter From the API documentation: public FileWriter(String filename) throws IOException The important part is the throws part! This constructor declares that it might throw an IOException for the reasons stated before. IOException is a checked exception (it doesn t extend RuntimeException), so any code calling it, must take action (or the compiler will complain).

What can we do then, to please the compiler? Calling something which declares that it might throw a checked exception gives you two choices: The method surrounding your code must declare that it throws an exception of the same sort (or a super class to the exception), or you must handle the exception. Declaring that your surrounding method also throws this exception is easy, simply use the throws keyword at the declaration, and it will be someone else s problem (the code which calls your method, that is).

If I want to handle the exception then? If you don t want to pass on the responsibility to the callers of your method, then you can handle the exception. This is used via the try-catch construct. You surround the code which might throw an exception with a try-block: try{ //code which might throw an exception! After the try-block you add a catch-block which handles the exception: catch(exceptionclassname ex){ //code which deals with the exception

Back to the writing-file example static void writefile(){ try{ PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt")); catch(ioexception ex){ System.err.println("Couldn't open file for writing: " + ex.getmessage()); The try-catch block handles IOExceptions if they are thrown: $ javac WritingFile.java && java WritingFile Couldn't open file for writing: OutFile.txt (Permission denied)

If we don t want to handle it in the method? If we didn t want to handle IOExceptions in the method, we could declare that the method throws IOException. But then, the method calling our method would be in our old situation and have to handle or declare the IOException. If the method is called from, e.g. main() then main() has to handle the IOException with a try-catch around the call to our method. Or it can declare that it throws IOException. Note, declaring that the main method throws exception isn t very helpful. No one can handle those exception and the program will crash instead.

Pause!

What can we do with the exception object? An object which we catch, of type Exception, has, among others, the following methods (which it has inherited): tostring() - you know what it does! getmessage() - it might have a message for us, as a String printstacktrace() - it can print the whole call stack, i.e. what method calls led up to this exception being thrown?

A message example: Our write file example did the following in the handler which caught an IOException and called it ex: System.err.println("Couldn't open file for writing: " + ex.getmessage()); An example printout was the following: Couldn't open file for writing: OutFile.txt (Permission denied) The message part was: OutFile.txt (Permission denied)

A stack trace example java.io.filenotfoundexception: OutFile.txt (Permission denied) at java.io.fileoutputstream.open(native Method) at java.io.fileoutputstream.<init>(fileoutputstream.java:221) at java.io.fileoutputstream.<init>(fileoutputstream.java:110) at java.io.filewriter.<init>(filewriter.java:63) at WritingFile.writeFile(WritingFile.java:11) at WritingFile.main(WritingFile.java:6) It shows the call chain (in reverse, because it s a stack): main->writefile->fw constructor->fos constructor x 2 -> open So, main() called writefile() which called the constructor which eventually called open() which crashed. And we g et line numbers too :-)

Pause...

Who should handle it? Why did we settle with handling the IOException in the writefile() method? Because, we knew that we couldn t do much more than anyone else could. If we are not allowed to write the file, there s not much we can do in the method to fix that. Problem is that now, main(), which called writefile() doesn t know that something really bad has happened. Perhaps that s fine. But if it was really important to write that file, even main should be notified. So should we declare that writefile() throws IOException then?

Perhaps passing the problem on isn t perfect If we had written the code like this instead: static void writefile() throws IOException{ PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt")); Then, main(), would ve been forced to write try-catch around the call to writefile(). Perhaps that s not so convenient (topic for discussion!). What alternatives do we have? The compiler is very particular about rules!

Throwing an exception We can throw an exception ourselves, using the keyword throw (as an imperative, without the trailing s). You have to say what you want to throw, and the thing you want to throw is an exception object. So this works: throw new IOException("A message about the problem"); We can also create an exception which wraps another exception: throw new RuntimeException("A message", ex); (where ex is a reference to the other exception, like an IOException)

Throwing a NullPointerException This seems useful, right? We can now enforce rules by documenting that we ll throw exceptions if people don t obey our rules. Considered this: public Member(String name, String email){ // Name is NOT allowed to be null!!! if(name == null){ throw new NullPointerException("Name cannot be null"); this.name = name; this.email = email; // we can live with null emails ;-)

Catching the IOException and rethrowing as other So, we could go back to our writefile() method and re-write it so that it doesn t declare that it throws any exception, handle any IOExceptions, and if we catch one, re-throw it as a RuntimeException (which doesn t need to be checked): static void writefile(){ try{ PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt")); catch(ioexception ex){ throw new RuntimeException("Problem writing: "+ex.getmessage(), ex);

What about main??? If main now calls writefile() and an IOException is caught and re-thrown as a RuntimeException (unchecked), main will have some problems. Unless main has a general exception handler, it will crash. So let s write a simplified general exception handler for main: public static void main(string[] args){ try{ writefile(); catch(exception e){ System.err.println("Something went wrong: " + e.getmessage()); System.exit(1); $ javac WritingFile.java && java WritingFile Something went wrong: Problem writing: OutFile.txt (Permission denied)

You should probably log the stacktrace as well It is possible to log the stacktrace in an error log, without showing the user the whole stacktrace. This is very helpful for developers who want to investigate what went wrong.

Pause

The finally clause There is another clause which you can use with a try-statement. The finally clause let s you add code to be executed regardless of whether an exception occurred or not. Consider (pseudo code, this is not real Java!): try{ turnonwater(); waterlawn(); catch(brokenpipeexception bpe){ calltheplumber(); finally{ turnoffwater();

Resources should be closed Don t leave the water running ;-) If we open a file, for instance, and try to do something with it, something might go wrong and we get an exception. A finally clause can be used to make sure that we remember to close the file regardless of whether an exception was thrown or not. This works even without the catch clause (but usually they are seen together). try{ something which may fail miserably finally{ something which must run regardless of any failure

Some words of advice Don t just catch and ignore. It makes your code compile and run, but nothing is reporting or trying to deal with the problem. Try to avoid to catch too broad an exception type. Usually, you want to do different things for different types of problems. Some code can often throw more than one type of exception. So you should catch and handle them separately in most cases.