CS102 – Exceptions David Davenport Latest: May 2015

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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.
For use of Cleveland State's IST410 Students only 1 Exception.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
11-Jun-15 Exceptions. 2 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.
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 Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
16-Jun-15 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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
COMP Exception Handling Yi Hong June 10, 2015.
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.
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.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling.
Object Throwable ErrorException RuntimeException.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Java Exceptions a quick review….
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Introduction to Exceptions in Java
CS Week 10 Jim Williams, PhD.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Advanced Programming Behnam Hatami Fall 2017.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Exception Handling Chapter 9.
Exceptions & exception handling
ATS Application Programming: Java Programming
Exceptions & exception handling
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 12 Exception Handling
Abdulmotaleb El Saddik University of Ottawa
Exception Handling.
Web Design & Development Lecture 7
Exceptions 19-Feb-19.
Lecture 11 Objectives Learn what an exception is.
Java Exceptions Dan Fleck CS211.
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
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.
Tutorial Exceptions Handling.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
Exception Handling Contents
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Exceptions 10-May-19.
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 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Exceptions Review Checked Vs. Unchecked Exceptions
Exceptions and Exception Handling
Presentation transcript:

CS102 – Exceptions David Davenport Latest: May 2015 Previous: May 2013, Spring 2002 See ExceptionPlay.java

Exceptions Handling run-time errors/problems check for and handle eg. Avoid divide by zero by testing denominator use exceptions consistent form uncluttered code compiler can help but maybe slower The OOP way!

Exception Class Hierarchy LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException ArrayIndexOutOfBoundsException several more classes All exceptions/errors are run-time! RunTimeExceptions are not checked by the compiler so programmer is not required to acknowledge them All other types require programmers to write code acknowledging a problem before the program will compile! RuntimeExceptions are unchecked Seriously bad news!

Exception Handling... Exceptions not handled in a method are passed up to calling method If not handled (caught) anywhere then program prints error message and, if console program, stops! (but GUI programs don’t...) … too hot to handle cartoon … (passing hot potato) Oven gloves & hot potatoes!

Can specify multiple catch clauses to handle different exception types Handling Exceptions Either “pass-the-buck” or handle them public void myMethod() throws ExceptionType { statements; } Unchecked exceptions don’t require attention, but can be caught & handled if desired. try { statements; } catch (ExceptionType e) { // handler for exception finally { // statements always done “throws” - pass the buck… handling the exception is someone else’s responsibility! - can list multiple exception types separated by commas. Any statements in the try part that follow the one that causes an exception are not done. (Optional) One or more catch clauses must list most specific catch clauses first since Java does the first matching one only. (Optional) finally clause… seems irrelevant, but may not be if the catch clauses themselves can cause an exception! Note: newer versions of Java, e.g. 1.7+, allow multiple exception types to be listed in catch clause, eg. catch ( IOException | SQLException ex) { … } allow try with resources, which are automatically closed afterwards “The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.” Can specify multiple catch clauses to handle different exception types Optional clause

Examples... Divide by zero try { System.out.println( 25/0); } catch (Exception e) { System.out.println( e); System.out.println( “wow..”); Stack calculator (see data structures...) In Stack class... public int pop() { if ( isEmpty() ) throw new StackUnderFlowException(); else return ...; } What happens if you change the exception type to ArithmeticException or InputMismatchException? What happens if you change add another catch clause for ArithmeticException –before/after this one?

Exceptions are relatively slow! Throwing Exceptions When error occurs simply throw a new exception Create your own Exception Types Exceptions are relatively slow! if ( x != y ) throw new MyExceptionType( “oops!” ); Can catch one type of exception and throw another. If ( n < 0) throw new IllegalArgumentException( “n must be positive”); Note: extend RunTimeException if programmmer not required to write code when using it! public class MyExceptionType extends Exception { public MyExceptionType( String message) { super( message); }

Files & Exception handling String filename; DataInputStream fin = null; try { System.out.print( "Enter filename: "); filename = scan.readLine(); fin = new DataInputStream( new FileInputStream( filename) ); while (true) { int i = fin.readByte(); System.out.print( i + "|" + (char) i + "| " ); } } catch ( FileNotFoundException e) { System.out.println( e + "\n--- Sorry, can't find a file with that name!" ); } catch ( IOException e ) { System.out.println( e + "\n--- The entire file has been processed!" ); fin.close(); } finally { System.out.println( "--- all done.."); Even though end of file is not exactly an exceptional circumstance, many languages, including Java, allow eof processing using exceptions. Note: readLine() in ??? class is the one “exception” to the rule; it doesn’t generate an exception (it returns null instead)!