Exceptions Handling the unexpected

Slides:



Advertisements
Similar presentations
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
Advertisements

Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
COMP 121 Week 5: Exceptions and Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
When you use an input or output file that does not exist, what will happen? −The compiler insists that we tell it what the program should do in such case.
Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
COMP Exception Handling Yi Hong June 10, 2015.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions and Assertions Chapter 15 – CSCI 1302.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
The Problem Unexpected erroneous situations are often discovered by code which is NOT prepared to remedy the error………. For example.. String in = JOptionPane.showInputDialog(“enter.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Generics, Exceptions and Undo Command
Tirgul 13 Exceptions 1.
Chapter 13 Exception Handling
Testing and Debugging.
Chapter 12 Exception Handling and Text IO
CSE 501N Fall ’09 17: Exception Handling
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
What/how do we care about a program?
Exception Handling Chapter 9.
Advanced Java Programming
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Exception Handling in Java
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Web Design & Development Lecture 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 13 Exception Handling
Lecture 11 Objectives Learn what an exception is.
Java Exceptions Dan Fleck CS211.
CMSC 202 Exceptions 2nd Lecture.
Exception and Event Handling
CSC 143 Java Errors and Exceptions.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Exceptions Handling the unexpected

Motivation So far, most of our code has been somewhat näive We have assumed that nothing goes wrong… User enters correct input We never address outside the boundaries of an array …and so on DCS – SWC

Motivation Of course, the real world works differently public class BankAccount { public void withdraw(double amount) if (amount > balance) // Now what? } ... DCS – SWC

Motivation Possible actions depend on our interpre-tation of this situation Legal – just do business logic Illegal, and we know what to do – perhaps just do nothing Illegal, and we do not know what to do! Error detection and error handling are often separated in code! DCS – SWC

Motivation Problem: Can become very complex to ”drag” error handling code around in code for business logic Error-handling may be application-specific GUI-application: Pop-up window Other: Write to a log file Business logic code should not choose strategy for error handling! DCS – SWC

Motivation Next problem: Error handling code is often very ”far away” from error detction main askUser doAction makeWithdraw withdraw ? Error detected Error handling DCS – SWC

Motivation Management of errors can be broken down into several tasks: Detection – realising an error situation has occurred Signaling – making the surrounding code aware that an error has been detected Capturing – taking responsibility for handling the error Handling – performing the error handling actions DCS – SWC

Exceptions The mechanism for crossing the gap of method calls is exceptions An exception in itself is ”just another class” We can create exception objects just as we can create other objects An exception object contains information about the type of error which occurred Java contains several built-in exception classes, forming an inheritance hierarchy DCS – SWC

Throwing and catching Exception can be thrown and catched What does that mean!? A very different flow than usual method calls An exception is thrown up through the chain of method calls DCS – SWC

Throwing and catching Who will catch the exception? main askUser doAction makeWithdraw withdraw Who will catch the exception? Error detected – throw! DCS – SWC

Throwing and catching A throw can look like this in Java: public void withdraw(double amount) { if (amount > balance) IllegalArgumentException ex = new IllegalArgumentException(”...”); throw ex; } balance = balance – amount; NOTE! DCS – SWC

Throwing and catching An exception is now thrown; this changes the flow of code immediately! Remaining code in the method throwing the exception is not executed Somebody must catch the exception In order to catch the exception, we must write an exception handler DCS – SWC

Throwing and catching General exception handler structure: try { // Code which may throw an exception ... } catch (ExceptionType ex) // Proper handling of exception DCS – SWC

Throwing and catching try { myAccount.withdraw(1000); myAccount.getTransactions(); ... } catch (IllegalArgumentException ex) System.out.println(ex.getMessage()); ex.printStackTrace(); DCS – SWC

Throwing and catching Things to note: Error detection (throw) and error handling (try/catch) is usually not in the same method The catch statement only catches exceptions of the specified type Information about the error is found implicitly – by the type of the exception – and explicitly from e.g the text stored in the object DCS – SWC

Throwing and catching Throw early, catch late! If you cannot fix a pro-blem correctly, throw an exception Only catch an exception if you really know how to fix the problem DCS – SWC

Throwing and catching try { myAccount.withdraw(1000); myAccount.getTransactions(); ... } catch (Exception ex) // do nothing... Tempting, but bad…! DCS – SWC

Checked and Unchecked How do I know what exceptions some piece of code can throw…? Difficult to code a method correctly without this knowledge Two types of exceptions exist Checked exception Unchecked exception DCS – SWC

Checked and Unchecked Checked exception Used for problems beyond the control of the programmer Corrupted file, network problems, etc.. Compiler insists that you explicitly decide what to do about it Option 1: Re-throw the exception Option 2: Handle the exception, using a catch clause matching the exception DCS – SWC

Checked and Unchecked // Suppose draw() can throw DrawException draw(Figure f); // Compiler will not like this! public void drawOne(Figure f) { getScreen().draw(f); } DCS – SWC

Checked and Unchecked // Option 1: Re-throw the exception // (i.e. do nothing…) public void drawOne(Figure f) throws DrawException { getScreen().draw(f); } DCS – SWC

Checked and Unchecked // Option 2: Handle the exception public void drawOne(Figure f) { try getScreen().draw(f); } catch (DrawException de) // Code for handling the problem DCS – SWC

Checked and Unchecked draw drawOne draw drawOne Option 1: drawOne does nothing, so it must annonce that it will (re)throw the exception draw drawOne Option 2: drawOne handles the exception, so it is ”consumed” by drawOne DCS – SWC

Checked and Unchecked Unchecked exception Used for problems which the programmer should be able to prevent Null reference, out of bounds reference,… Why do we have these…? Accidents do happen…! Unchecked exceptions are not announced DCS – SWC

The finally Clause Sometimes we need to execute some specific code after an exception occurs Typically ”clean-up” code – close a file connection, a database connection, etc. Where do we put this code…? In exception handlers? Difficult, who actually catches the exception… In a finally clause! DCS – SWC

The finally Clause PrintWriter out = new PrintWriter(filename); ... try { writeData(out); } finally // This code will always be executed, // even if the above code throws an exception out.close(); DCS – SWC

The finally Clause The code in the finally clause is guaran-teed to be executed, in one of these ways: If no exceptions are thrown: After completing the last statement in the try block If an exception is thrown: Execute code in finally clause Exit to exception handler DCS – SWC

The finally Clause NOT SO GOOD GOOD try { } catch finally try { } DCS – SWC

Making your own exceptions Throw exceptions that are as specific as possible – also in terms of type Many built-in exceptions to choose from Can be appropriate to create your own exceptions Just extend existing class DCS – SWC

Making your own exceptions public class InsufficientFundsException extends RunTimeException { public InsufficientFundsException() {} public InsufficientFundsException(String message) super(message); } DCS – SWC

Exceptions vs. Flow control Exceptions change the linear flow of code, just like if, while, etc. However, they are only intended for error detection and handling Do not use exceptions as a substitute for ordinary flow control DCS – SWC

Exceptions vs. Flow control GOOD for (int i = 0; i < noOfElements; i++) myArray[i] = i; try { for (int i = 0; /* No Test?? */ ; i++) } catch (ArrayIndexOutOfBoundsException e) {} BAD DCS – SWC

Exceptions summary Throw early, catch late Only catch, if you can handle the problem correctly You must deal with checked exceptions Use try, catch and finally appropriately Make your own exception classes, if you really need them Exceptions are not for flow control DCS – SWC