COMP1008 Exceptions. Runtime Error

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

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

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

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.

Object Oriented Programming. Week 7 Part 1 Exceptions

ITI Introduction to Computing II

ITI Introduction to Computing II

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

Exception Handling Generics. Amit Gupta

CS 3 Introduction to Software Engineering. 3: Exceptions

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

Object Oriented Programming

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

Assertions and Exceptions Lecture 11 Fall 2005

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

BBM 102 Introduction to Programming II Spring Exceptions

Errors and Exceptions

Program Correctness and Efficiency. Chapter 2

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

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

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

BBM 102 Introduction to Programming II Spring 2017

Exception-Handling Overview

More on Exception Handling

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

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

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

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

What can go wrong in a Java program while running?

Internal Classes and Exceptions

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

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

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

C16b: Exception Handling

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

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

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

Sri Vidya College of Engineering & Technology Question Bank

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

More on Exception Handling

Exceptions - Example. Exceptions - Example

CS112 Lecture: Exceptions and Assertions

6.Introducing Classes 9. Exceptions

Recitation 3. 2D Arrays, Exceptions

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

ECE 122. Engineering Problem Solving with Java

Chapter 13 Exception Handling

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

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

Lecture 19 Programming Exceptions CSE11 Fall 2013

Programming II (CS300)

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

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

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

Full file at Chapter 2 - Inheritance and Exception Handling

Object Oriented Programming Exception Handling

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

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

Exceptions. CSC207 Winter 2017

Programming Languages and Techniques (CIS120)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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


OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

Defensive Programming. Ric Glassey

CSCI 261 Computer Science II

Programming II (CS300)

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

Exceptions and Design

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

CS Programming I: Exceptions

Data Structures. 02 Exception Handling

Introduction to Software Design

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:

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

CS193j, Stanford Handout #25. Exceptions

2.6 Error, exception and event handling

Object Oriented Programming

Java Exceptions Version June 2009

Fundamentals of Object Oriented Programming

Preconditions. CMSC 330: Organization of Programming Languages. Signaling Errors. Dealing with Errors

CS Programming I: Exceptions

Introduction to Java. Handout-3a. cs402 - Spring

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

Exceptions and Error Handling

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

Principles of Software Construction: Objects, Design and Concurrency. Exceptions, scope, static data and methods, and Generics.

Exceptions in Java

Correctness and Robustness

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

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

Java Loose Ends. 11 December 2017 OSU CSE 1

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

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

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

Abstract Classes, Exceptions

Transcription:

Runtime Error COMP1008 Exceptions Unexpected error that terminates a program. Undesirable Not detectable by compiler. Caused by: Errors in the program logic. Unexpected failure of services E.g., file server goes down. 2 Exception example Call Stack Trace int x = 1 ; int y = 0 ; int z = x / y ; java.lang.arithmeticexception: / by zero at T9.main(T9.java:7) This message is displayed when the code is executed. You typically see a call stack trace. apple.awt.eventqueueexceptionhandler Caught Throwable : java.lang.arithmeticexception: / by zero java.lang.arithmeticexception: / by zero at uk.ac.ucl.cs.editor.memolisteditor.backward(memolisteditor.java:60) at uk.ac.ucl.cs.view.swingmemolistview$4.actionperformed(swingmemolistview.java:173) at javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:1819) at javax.swing.abstractbutton$forwardactionevents.actionperformed(abstractbutton.java:1872) at javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:420) at javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:258) at javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:247) at java.awt.component.processmouseevent(component.java:5166) at java.awt.component.processevent(component.java:4963) at java.awt.container.processevent(container.java:1613) at java.awt.component.dispatcheventimpl(component.java:3681) at java.awt.container.dispatcheventimpl(container.java:1671) at java.awt.component.dispatchevent(component.java:3543) at java.awt.eventdispatchthread.run(eventdispatchthread.java:100) etc. 3 4 Null pointer exception Stack Should be familiar... java.lang.nullpointerexception at T10.main(T10.java:7) A stack is a last-in first-out queue: Items are pushed on, and popped off the pile. Can use an ArrayList to store stack contents. Pop Push (So references are really pointers according to the JVM!) Top of stack 5 6

Class Stack class Stack<T> private ArrayList<T> contents; public Stack() contents = new ArrayList<T>(); // Methods push, pop, top... Stack<Integer> = new Stack<Integer>(); This is a generic Stack class. T is a type variable that is instantiated with a real type in a declaration. Programmers make mistakes! Stack<Double> s = new Stack<Double>() ; // Empty stack Double d = s.pop() ; // Whoops!! System.out.println(s.top()) ; // What is printed? How can you force someone to take notice when these kinds of errors are made? Best answer: Use test-first programming. 7 8 (Non) Solution 1 Solution 2 Ignore the problem and hope it never happens! Relies on object always being used correctly... public Object pop() Print out an error message. System.out.println( Stack empty ); return null; return contents.remove(0); What value is returned? Caller must deal with null being returned. Message may be noticed but which call of pop caused the problem? 9 10 Solution 3 Solution 4 Stop the program! public T pop() Bad design to put output into data structure class like this. Don't do it! System.out.println( Stack is empty ) ; System.exit(1) ; // Force program to stop return null; // Needed to compile but not used return contents.remove(0); Can t avoid noticing this! But still little information about where and why. public T pop() return null; // silent default action if empty return contents.remove(0); Take some default action and rely on client code doing right thing with result. But moves problem somewhere, without compiler checking correctness. 11 12

Solution 5 Java Exception Mechanism Throw an exception! public T pop() throws EmptyStackException throw new EmptyStackException(); // Note no return needed here return contents.remove(0); Force the program to deal with the error or terminate. Force the compiler to check code. Force the programmer to write the code properly. To allow program execution to continue after an error. Or, at least, terminate gracefully. Uses five keywords in the language:, catch, throw, throws, finally 13 14 Try and catch a.dosomething() ; catch (Exception e) // Handle the exception A block tries to execute statements. A catch block catches exceptions thrown from the block. Throw throw new MyException ( Method dosomething failed ) ; The throw statement throws an exception. It takes an exception object reference as an argument. Somewhere a catch block must catch the exception. 15 16 Catch Multiple catch expressions catch (Exception e)... This catches an object of library class Exception or any of its subclasses. Typically, your exceptions are subclasses of class Exception. catch (MyException e)... Several catch expressions can be given to catch a range of exception objects of different classes: catch (MyException e1) catch (NumberFormatException n) catch (InvalidDataException i) A subclass extends another class. Exception represents exceptions in general, a subclass represents a specific kind of exception like EmptyStackException. 17 18

Finally finally f() ; catch (MyException e) // optional // Do something finally // Guaranteed to execute // this whatever happens. A finally block will be always be executed regardless of what happens. Standard Exception Classes Throwable Superclass of all exception classes. Error (extends Throwable) Serious error that is not usually recoverable. Exception (extends Throwable) Error that must be caught and recovered from. RuntimeException (extends Exception) Error that may be caught if desired. 19 20 class Throwable Writing an exception class Throwable provides: A String to store a message about an exception. A method String getmessage() to return the message string. A method printstacktrace. And a few other methods. Subclasses extend Throwable and can add further variables and methods. Most, but not all, Exception classes represent exceptions that must be caught. The compiler will check. A small number of Exception classes represent exceptions that do not need to be caught. class MyException extends Exception public MyException () super( Default message ) ; public MyException (String s) super (s) ; The extends keyword specifies that MyException is a subclass of Exception. We will be looking at subclasses in detail later in the course. 21 22 Questions? An Example public void f(string s) // s should represent an integer. int tmp ; tmp = Integer.parseInt(s) ; // This can fail catch (NumberFormatException e) tmp = -1 ; // Set tmp to some default value // Carry on and use tmp 23 24

Integer.parseInt Throws keyword public static int parseint (String s) throws NumberFormatException return parseint(s,10) ; parseint is overloaded and calls another version of parseint that can throw an exception. This version does not catch the exception but declares that it can occur. A method can directly or indirectly throw an exception without catching it. the exception must be declared by a throws declaration, for class Exception and subclasses (excluding RuntimeException). If not, the compiler will fail the code. Guarantees that exception will be used correctly. 25 26 Integer.parseInt (2) Integer.parseInt (3) public static int parseint(string s, int radix) throws NumberFormatException if (s == null) throw new NumberFormatException("null"); if (radix < Character.MIN_RADIX) throw new NumberFormatException ("radix " + radix + " less than Character.MIN_RADIX"); For every invalid state detected in the method body an exception is thrown. The method does not attempt to catch its own exceptions. Catching is left to the caller of the method. Or caller of the caller of the method 27 28 Integer.parseInt (4) Integer.parseInt (5) When an exception is thrown the method terminates immediately. without returning a value in the normal way. The exception is thrown back to the calling method. The caller method either: catches the exception. or it terminates immediately and throws the exception back to its caller. parseint(string s, int radix) Call parseint(string s) Call Error Throw Throw on without catching f() Catch and handle 29 30

Propagating exceptions Uncaught Exceptions Passing an exception up through a chain of active method calls is called propagation. The active methods calls are those still in progress, leading to the point where the exception occurred. If an exception is passed back to the main method without being caught the program will terminate with an error. However, program won t compile if exception must be caught. I.e., subclass of Exception (excl. RuntimeException). Good design and testing practice will avoid this happening... 31 32 Stack class revisited Stack client Define a StackException class. Any Stack method that can fail should throw an exception (e.g., empty/full stack). Stack does not catch its own exceptions. Clients of Stack must be prepared to catch the exceptions. The calling method, or a method that calls it, must have a catch block for the exception. public void amethod(stack<string> astack) String s; s = astack.pop(); catch (StackException e) // Do something to recover // from problem //... rest of method or public void amethod(stack<string> astack) throws StackException String s = astack.pop(); //... rest of method The method or a method that calls it must contain the catch block. 33 34 When to use Exceptions Issues The normal sequence of events fails. I/O and user action. Method cannot proceed and there is no practical return value (e.g., parseint). Need to return control to a method at top of call stack. Not a substitute for using return. Too many exceptions require too many /catch blocks. Complicates code. Can reduce readability. Many methods need throws declaration. But can simplify. Some code is written assuming no errors, so simpler. A balance is needed. 35 36

Local v. Global More Information Some local operation (parseint, open file) may fail. Deal with problem locally and proceed. Top level method(s) catch exceptions from anywhere in program. Terminate current operation but leave program running. Top level strategy for handling errors (e.g., save data). Read the text book. 37 38 Summary Exceptions allow errors to be represented and handled in a safe way. Java uses the, catch & throw mechanism. Throwing an exception forces client code to do something. Don t forget finally. 39