Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/

Slides:



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

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
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.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
For use of Cleveland State's IST410 Students only 1 Exception.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exceptions and Exception Handling Carl Alphonce CSE116.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
Java Exceptions, Cloning, Serialization Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
交通大學資訊工程學系 Programming in Java Exception Handling again 蔡文能 交通大學資訊工程學系
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 CIS 304 Intermediate Java Programming for Business.
交通大學資訊工程學系 Programming in Java Exception Handling 蔡文能 交通大學資訊工程學系
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.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
Java Programming: Guided Learning with Early Objects
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
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.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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.
CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
Java Exceptions a quick review….
CS102 – Exceptions David Davenport Latest: May 2015
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.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
ATS Application Programming: Java Programming
Exception Handling in Java
CS201: Data Structures and Discrete Mathematics I
Web Design & Development Lecture 7
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.
Lab 1 Exception Handling.
Presentation transcript:

Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/

Java Programming: Exceptions2 Issues What to do when you catch an exception? How and when to generate exceptions. RunTime exceptions. Custom Exception types. Using finally.

Java Programming: Exceptions3 Exception Reminder try { readFromFile("datafile"); } catch (FileNotFoundException e) { System.err.println("Error: File not found"); } try { readFromFile("datafile"); } catch (FileNotFoundException e) { System.err.println("Error: File not found"); }

Java Programming: Exceptions4 Exception Handling: Some Options Print something Throw a new exception Re-Throw the exception Fix the problem Exit

Java Programming: Exceptions5 Exception Handling: Printing You can print a stack trace by calling the exception method printStackTrace() Sometimes it's better to send error messages to stderr : –System.err.println("Error: invalid thingy"); Some applications log error messages –file –logging service (syslog).

Java Programming: Exceptions6 Exception Handling: throw You can throw an exception from an exception handler (a catch block). Allows you to change exception type and/or error message. You can also alter the base of the stack trace fillInStackTrace()

Java Programming: Exceptions7 Exception Handling: Re- throw You can throw an exception from an exception handler (a catch block) without changing anything: called rethrowing The caller needs to deal with the exception. This also happens if you don't catch the exception! sometimes you need to take some action and then rethrow the exception.

Java Programming: Exceptions8 Another way to re-throw You can allow selected types of exceptions to be propogated to the caller of your method: void blah() throws IOException { Within blah() you don't need to catch these exceptions (to be able to compile).

Java Programming: Exceptions9 Exception Handling: Fix the problem. You can't fix things and then resume execution automatically you can do this in C++. You can have a loop the retries the code again. Sample code: Wait.javaWait.java

Java Programming: Exceptions10 Exception Handling: exiting Sometimes the error is fatal, and you want to stop the program immediately. System.exit(); Sample code: Wait.javaWait.java

Java Programming: Exceptions11 How/when do you generate exceptions? Use throw: throw new Exception("broken!"); You can use throw anywhere. –you detect some error that means the following code should not be executed. In some cases, you can think of throw as a alternate return

Java Programming: Exceptions12 Exception Enforcement In general, you do the following: –specify what exceptions each method can generate. –write code to catch all exceptions that can be generated by a method call. The compiler (usually) enforces this –it is a compilation error to call a method without catching it's declared exception types.

Java Programming: Exceptions13 RunTime Exceptions There are exceptions that are generated by the system (that are usually caused by programming mistakes): –NullPointerException (null references) –ArrayIndexOutOfBoundsException If you don't catch these, a stack trace will be generated and the program will terminate. The compiler does not force you to catch these exceptions.

Java Programming: Exceptions14 Exception Types Exceptions are objects! Exception types are classes. –A (quite large!) hierarchy of classes. All exception types are derived from the class Exception –there are some methods defined in this base class.

Java Programming: Exceptions15 Throwable Exception Error RunTimeException NullPointerException ArithmeticException IOException Exception Type Hierarchy (partial) EOFException VirtualMachineError

Java Programming: Exceptions16 Some Exception Methods These are actually inherited from throwable printStackTrace() fillInStackTrace() getMessage()

Java Programming: Exceptions17 Creating Your Own Exception Types It is often useful to create your own type of exception. –generally all you create is a name. –you can get fancy and add new methods to your exception class(es).

Java Programming: Exceptions18 Custom Exception Type class FooException extends Exception {} class BlahException extends Exception { BlahException(){} BlahException(String s) { super(s); } } throw new BlahException("Invalid blah"); Sample code: Batlship.java in cis421\java\BattleShip\Exception\Caught\Polymorphic

Java Programming: Exceptions19 using finally try { statements... } catch (ExceptionType1 ename1) { error handling statements... } catch (ExceptionType2 ename2) { error handling statements... } finally { … this code always executed … }

Java Programming: Exceptions20 Why finally ? What is there to clean up? –No memory cleanup required in Java! –No destructors to call! Sometimes you need to set the state of things (fields) to some stable (acceptable) state.