CS 2430 Day 22. Announcements Prog4 test document and Rational Junit tests are due this Friday at 2 PM Quiz 3 this Friday Quiz 4 next Friday Exam 2: 4/3/13.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Yoshi
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
For use of Cleveland State's IST410 Students only 1 Exception.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
Applets & Applications CSC 171 FALL 2001 LECTURE 15.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
CS1101: Programming Methodology Aaron Tan.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
1 Features of Java (2) CS 3331 Sections 4.5 and 4.6.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Introduction to Exceptions in Java
CS102 – Exceptions David Davenport Latest: May 2015
Creating and Modifying Text part 2
Handling Exceptions.
Advanced Programming Behnam Hatami Fall 2017.
ATS Application Programming: Java Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
Web Design & Development Lecture 7
Exception Handling in Java
Java Exceptions Dan Fleck CS211.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Tutorial Exceptions Handling.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Exceptions and Exception Handling
Presentation transcript:

CS 2430 Day 22

Announcements Prog4 test document and Rational Junit tests are due this Friday at 2 PM Quiz 3 this Friday Quiz 4 next Friday Exam 2: 4/3/13 –Review for exam 2 in lab on 4/2/13

Agenda Exceptions

Motivation public static void main(String args[]) { Stack theStack = new Stack(); Object obj = theStack.pop(); // Uh oh, pop from empty Stack!?! } What to do here? Just let the program crash? ($5) The pop() method could return null ? ($10) Use Exception s! (>$50)

The Exception class In Java, Exception s are Object s Can be “thrown” ( throw, throws ) Must be “caught” ( try, catch, finally )

The Exception hierarchy Exception RuntimeExceptionIOException... ArrayOutOfBoundsException... Throwable Object

Handling Exception s

Does this look familiar? try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } Let’s take a closer look!

The try block try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } Contains code that may throw an Exception, e.g. run()

The “suspect” code try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } Remember the signature for run() ? public void run() throws IOException

A catch block try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } If an IOException is thrown, it is caught by this (matching) catch block.

Handle the IOException try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } If IOException is thrown by run(), this code is executed

The finally block try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } finally { System.out.println("Always executed"); } The (optional) finally block is always executed last

Exception handling rules A try block can have one or more catch blocks If an Exception is thrown, only the first matching catch block is executed If there is a finally block, it must be last –And it is always executed! Do NOT try - catch RuntimeExceptions ( NullPointerException, etc.)

Be careful! try {... } catch (Exception ex) { System.out.println("Exception"); } catch (IOException ioex) { System.out.println("IOException"); }

Be careful! try {... } catch (Exception ex) { System.out.println("Exception"); } catch (IOException ioex) { System.out.println("IOException"); } Syntax error! Why?

Children first, parents last try {... } catch (IOException ioex) { System.out.println("IOException"); } catch (Exception ex) { System.out.println("Exception"); } “Children must be caught before parents”

Defining your own Exception s

Just make a new class public class StackEmptyException {... // could have data public StackEmptyException(String msg) {... } } Not quite an Exception yet

Our very own Exception ! public class StackEmptyException extends Exception {... public StackEmptyException(String msg) { super(msg);... } } Most of the time, this is all we need for our own Exception s

When to throw a StackEmptyException ?

Before public class Stack { private Object[] items; private int top;... public Object pop() { return items[--top]; } }

After public class Stack { private Object[] items; private int top;... public Object pop() throws StackEmptyException { if (top == 0) throw new StackEmptyException("Empty stack, ya'll!"); return items[--top]; }

Exception may be thrown public class Stack { private Object[] items; private int top;... public Object pop() throws StackEmptyException { if (top == 0) throw new StackEmptyException("Empty stack, ya'll!"); return items[--top]; } The throws keyword specifies that a StackEmptyException may be thrown (back to the calling method)

throw an Exception public class Stack { private Object[] items; private int top;... public Object pop() throws StackEmptyException { if (top == 0) throw new StackEmptyException("Empty stack, ya'll!"); return items[--top]; } This statement throw s a new StackEmptyException back to where the method was initially called

Client code public void doSomething(Stack inStack) {... Object obj = inStack.pop(); System.out.println("Here is the object: " + obj);... } This is (now) a syntax error!

Must try - catch public void doSomething(Stack inStack) {... try { Object obj = inStack.pop(); System.out.println("Here is the object: " + obj); } catch (StackEmptyException ex) { System.out.println("Here is the exception: " + ex); }... }

Could also do this public void doSomething(Stack inStack) throws StackEmptyException {... Object obj = inStack.pop(); System.out.println("Here is the object: " + obj);... } In general: If the calling method does not try - catch the Exception, it must advertise that it throws the Exception to the calling method.

Bad practice public static void main(String args[]) throws Exception {... } You probably shouldn’t do this. Why?

Do NOT throw or throws RuntimeException s ( NullPointerException, etc.)