COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Yoshi
Advertisements

Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
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.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Exceptions Exception. Exceptions Overview Exceptional Situations Syntax of Try, Catch, Throw Defining and Using Your Own Exceptions.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Testing and Error Handling Intro to Java. Testing We test to try and make sure our programs work correctly and have no bugs If we have access to the code,
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
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.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
CS1101: Programming Methodology Aaron Tan.
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.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
1 CSC241: Object Oriented Programming Lecture No 27.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
1 Features of Java (2) CS 3331 Sections 4.5 and 4.6.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
CSE 1201 Object Oriented Programming
Advanced Programming in Java
Lecture 14 Throwing Custom Exceptions
Exceptions In this lecture:
Chapter 10 – Exception Handling
Advanced Programming in Java
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
MIT AITI 2003 Lecture14 Exceptions
Java Programming Language
Introduction to Exceptions in Java
Exceptions, Interfaces & Generics
Introduction to Exceptions in Java
CS102 – Exceptions David Davenport Latest: May 2015
Week 14 - Wednesday CS 121.
Creating and Modifying Text part 2
Multithreading in Java
CS Week 10 Jim Williams, PhD.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Advanced Programming Behnam Hatami Fall 2017.
Exceptions & exception handling
ATS Application Programming: Java Programming
Exceptions & exception handling
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Exception Handling CSCI293 - C# October 3, 2005.
COMPUTER 2430 Object Oriented Programming and Data Structures I
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling in Java
Exception Handling in Java
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Abstract Data Types (ADTs) Stacks
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
A type is a collection of values
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Exceptions

Class Stack public class Stack { private Object[] items; private int top = 0; public Object pop() return items[-- top]; } ... What if the stack is empty (top == 0)? Crash!

Class Stack public class Stack { private Object[] items; private int top = 0; /** Must call isEmpty before calling pop! */ public Object pop() return items[-- top]; } ... Can this avoid all crashes? NO!

Java Class Exception To avoid as many crashes as possible Throws exception objects Try and Catch

Throwing Exceptions public class Stack { private Object[] items; private int top = 0; /** Should make sure it’s not empty before calling pop. An exception will be thrown if empty. */ public Object pop() if ( top == 0 ) throw new Exception(“Empty Stack!”); return items[-- top]; } ...

Throwing Exceptions public class Stack { private Object[] items; private int top = 0; /** Should make sure it’s not empty before calling pop. An exception will be thrown if empty. */ public Object pop() throws Exception if ( top == 0 ) throw new Exception(“Empty Stack!”); return items[-- top]; } ...

Catching Exceptions Stack myStack = new Stack(100); Object obj; ... try { obj = myStack.pop(); // public Object pop() throws Exception // statement NEXT } catch ( Exception e ) System.out.println( e.toString() ); Won’t crash! Method pop is aborted. Statement NEXT will not be executed.

Try-Catch-Finally Stack myStack = new Stack(100); Object obj; ... try { obj = myStack.pop(); } catch ( Exception e ) System.out.println( e.toString() ); finally // Statements will always be executed, // even no exception is caught. Quiz is coming? Program due!

Method and Try-Catch-Finally public void DoWork( Stack myStack ) { try obj = myStack.pop(); } catch ( Exception e ) System.out.println( e.toString() ); // public Object pop() throws Exception finally ... // Clean up DoWork() handles the Exception! No “throws” on the method header. Quiz is coming? Program due!

Method and Try-Catch-Finally public void MoreWork() { Stack myStack; ... DoWork( myStack ); // public void DoWork( Stack myStack ) } Don’t need Try-Catch for Stack The exception is handled inside DoWork(). Quiz is coming? Program due!

Method and Try-Catch-Finally public void DoWork( Stack myStack ) throws Exception { ... obj = myStack.pop(); // public Object pop() throws Exception } DoWork() has no try-catch. Must throw Exception! Quiz is coming? Program due!

Try-Catch and Throws public void MoreWork() { Stack myStack; ... DoWork( Stack myStack ); //public void DoWork( Stack myStack ) throws Exception } Must Try-Catch Or Throws Exception Quiz is coming? Program due!

Class Exception Exception ArrayIndexOutOfBoundsException NullPointerException Other Exceptions User Exceptions Sub-Exceptions

Multiple Exceptions public void MyMethod() throws NullPointerException, ArrayIndexOutOfBoundsException { ... if ( . . . ) throw new ArrayIndexOutOfBoundsException(“Out of bounds”); else if ( . . . ) throw new NullPointerException(“No Object”); else } Quiz is coming? Program due!

Try-Catch-Finally try { ... } catch (ArrayIndexOutOfBoundsException e ) System.out.println( e.toString() ); catch (NullPointerException e ) ... // More catch blocks can be here finally Quiz is coming? Program due!

Try-Catch-Finally // Other exceptions will not be caught try { ... } catch (ArrayIndexOutOfBoundsException e ) System.out.println( e.toString() ); catch (NullPointerException e ) finally Quiz is coming? Program due!

Try-Catch-Finally // Catch all exceptions! try { ... } catch (ArrayIndexOutOfBoundsException e ) catch (NullPointerException e ) catch (Exception e ) finally Quiz is coming? Program due!

User Defined Exceptions public class StackEmptyException extends Exception { . . . // could have data public StackEmptyException ( String s ) super(s); . . . } . . . // We do not do this in CS 2430. Quiz is coming? Program due!

Throw StackEmptyException public class Stack { private Object[] items; private int top = 0; public Object pop() throws StackEmptyException if ( top == 0 ) throw new StackEmptyException (“Empty Stack!”); return items[-- top]; } ...

Catch StackEmptyException public void DoWork( Stack myStack ) { try obj = myStack.pop(); } catch (StackEmptyException e ) // catch (Exception e ) System.out.println( e.toString() ); finally ... // Clean up Quiz is coming? Program due!

Throws StackEmptyException public void DoWork( Stack myStack ) throws StackEmptyException { ... obj = myStack.pop(); } Quiz is coming? Program due!

Exceptions If a method throws an exception The header must indicate that public Object pop() throws Exception If a method calls another method that throws an exception Two options: 1. Try and Catch, or 2. The header must indicate that public void DoWork(Stack myStack) throws Exception

Static Main() public class Prog3 { ... public static void main( String [] args ) Scanner sc; try // throws an Exception if file not there sc = new Scanner( new File("Prog3_1.in") ); } catch (Exception ex) sc = new Scanner( System.in ); Quiz is coming? Program due!

Parameter args of Main() public class Prog3 { ... public static void main( String [] args ) String inFile = args[0]; String outFile = args[1]; } // Command line java Prog3 p3.in p3.out // args.length: 2 // args[0] : “p3.in” // args[1] : “p3.out” Quiz is coming? Program due!

Exercise public class TryExceptions { public void f1() throws Exception System.out.println("In f1"); throw (new Exception("F1 Exception")); } ... Quiz is coming? Program due!

public class TryExceptions { public void f1() throws Exception System public class TryExceptions { public void f1() throws Exception System.out.println("In f1"); throw (new Exception("F1 Exception")); } public void f2() try System.out.println("In f2"); f1(); catch ( Exception e ) System.out.println("Caught in f2"); Quiz is coming? Program due!

public class TryExceptions { public static void main(String [] arrgs) TryExceptions tr = new TryExceptions(); int x = 1, y = 1; while ( x < 4 && y < 4) try if ( x < 2 ) tr.f1(); tr.f2(); if ( x < 3 ) tr.f1(); System.out.println("At the bottom"); } catch ( Exception e ) ++ x; System.out.println( "Caught in Main" ); ++ y; System.out.println( "X: " + x + " Y: " + y ); Quiz is coming? Program due!

Schedule Lab 8 (5 points) Lab 7 (5points) Prog4 (20 points) Quiz 4 Due 11 pm, October 31 Lab 7 (5points) Due 11 pm, November 2 Prog4 (20 points) Due 11 pm, November 9 Quiz 4 Monday, November 5 Test 2 Wednesday, November 14