„Exceptions”. Exceptions Handling errors with exceptions Golden rule of programming: Errors occur in software programs. What really matters is what happens.

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

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
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.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
COMP 121 Week 5: Exceptions and Exception Handling.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
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.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
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.
Handling Errors with Exception (in Java) Project 10 CSC 420.
Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Advanced Java Programming and Database connection Eran.
Java Exception Handling ● Exception = an event that occurs during the execution of a program that disrupts the normal flow of instructions: – Examples:
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Java Software Solutions Foundations of Program Design Sixth Edition
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
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.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
CS 2511 Fall  Exception = an event that occurs during the execution of a program that disrupts the normal flow of instructions:  Examples: Out.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
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 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.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with 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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Java Exceptions a quick review….
Exceptions In this lecture:
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
Exceptions 10-Nov-18.
Advanced Programming Behnam Hatami Fall 2017.
Intro to Exceptions (c) Eraj Basnayake
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.
Exceptions 10-May-19.
Java Basics Exception Handling.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

„Exceptions”

Exceptions Handling errors with exceptions Golden rule of programming: Errors occur in software programs. What really matters is what happens after the error occurs: How is the error handled? Who handles it? Can the program recover, or should it just die?

Exceptions Exceptions - fundamentals An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Many kinds of errors can cause exceptions--problems ranging from serious hardware errors, such as a hard disk crash, to simple programming errors, such as trying to access an out-of-bounds array element. When such an error occurs within a Java method, the method creates an exception object and hands it off to the runtime system. The exception object contains information about the exception, including its type and the state of the program when the error occurred. The runtime system is responsible for finding some code to handle the error. In Java terminology, creating an exception object and handing it to the runtime system is called throwing an exception.

Exceptions Exceptions – fund...(2) After a method throws an exception, the runtime system leaps into action to find someone to handle the exception. The set of possible "someones" to handle the exception is the set of methods in the call stack of the method where the error occurred. The runtime system searches backwards through the call stack, beginning with the method in which the error occurred, until it finds a method that contains an appropriate exception handler. An exception handler is considered appropriate if the type of the exception thrown is the same as the type of exception handled by the handler. The exception handler chosen is said to catch the exception. If the runtime system exhaustively searches all of the methods on the call stack without finding an appropriate exception handler, the runtime system (and consequently the Java program) terminates.

Exceptions Exceptions advantages 1. Separating error handling code from „regular” code. Suppose that you have a function that reads an entire file into memory. In pseudo-code, your function might look something like this: readFile { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } At first glance this function seems simple enough, but it ignores all of these potential errors: What happens if the file can't be opened? What happens if the length of the file can't be determined? What happens if enough memory can't be allocated? What happens if the read fails? What happens if the file can't be closed?

Exceptions Exceptions advantages(2) Your function would looking something like this: errorCodeType readFile { initialize errorCode = 0; open the file; if (theFileIsOpen) { determine the length of the file; if (gotTheFileLength) { allocate that much memory; if (gotEnoughMemory) { read the file into memory; if (readFailed) { errorCode = -1; } } else { errorCode = -2; } } else { errorCode = -3; } close the file; if (theFileDidntClose && errorCode == 0) { errorCode = -4; } else { errorCode = errorCode and -4; } } else { errorCode = -5; } return errorCode; }

Exceptions Exceptions advantages(3) If your read_file function used exceptions instead of traditional error management techniques, it would look something like this: readFile { try { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } catch (fileOpenFailed) { doSomething; } catch (sizeDeterminationFailed) { doSomething; } catch (memoryAllocationFailed) { doSomething; } catch (readFailed) { doSomething; } catch (fileCloseFailed) { doSomething; } } The bloat factor for error management code is about 250 percent--compared to 400 percent in the previous example.

Exceptions Exceptions advantages(4) 2. Propagating errors up the call stack Lets suppose than only method1 is interested in errors occured during reading the file: method1 { call method2; } method2 { call method3; } method3 { call readFile; } Traditional error notification techniques force method2 and method3 to propagate the error codes returned by readFile up the call stack until the error codes finally reach method1- the only method that is interested in them.

Exceptions Exceptions advantages(5) method1 { errorCodeType error; error = call method2; if (error) doErrorProcessing; else proceed; } errorCodeType method2 { errorCodeType error; error = call method3; if (error) return error; else proceed; } errorCodeType method3 { errorCodeType error; error = call readFile; if (error) return error; else proceed; }

Exceptions Exceptions advantages(6) The Java runtime system searches backwards through the call stack to find any methods that are interested in handling a particular exception. Thus only the methods that care about errors have to worry about detecting errors. method1 { try { call method2; } catch (exception) { doErrorProcessing; } } method2 throws exception { call method3; } method3 throws exception { call readFile; }

Exceptions Exceptions advantages(7) 3. Grouping Error Types and Error Differentiation. Lets imagine a group of exceptions, each of which represents a specific type of error that can occur when manipulating an array: the index is out of range for the size of the array, the element being inserted into the array is of the wrong type, or the element being searched for is not in the array. For example ArrayException is a subclass of Exception (a subclass of Throwable ) and has three subclasses:

Exceptions Exceptions advantages(8) An exception handler that handles only invalid index exceptions has a catch statement like this: catch (InvalidIndexException e) {... } To catch all array exceptions regardless of their specific type, an exception handler would specify an ArrayException argument: catch (ArrayException e) {... } It is possiblel to set up an exception handler that handles any Exception with this handler: catch (Exception e) {... } It is not recommended writing general exception handlers as a rule.

Exceptions Understanding exceptions Let's look at following code: public class InputFile { private FileReader in; public InputFile(String filename) { in = new FileReader(filename); } What should the FileReader do if the named file does not exist on the file system? Should the FileReader kill the program? Should it try an alternate filename? Should it just create a file of the indicated name? There's no possible way the FileReader implementers could choose a solution that would suit every user of FileReader. By throwing an exception, FileReader allows the calling method to handle the error in way is most appropriate for it.

Exceptions Catch or specify requirement Java requires that a method either catch or specify all checked exceptions that can be thrown within the scope of the method. Catch - a method can catch an exception by providing an exception handler for that type of exception. Specify - if a method chooses not to catch an exception, the method must specify that it can throw that exception.

Exceptions Runtime and checked exceptions Runtime exceptions are those exceptions that occur within the Java runtime system. This includes exceptions like: arithmetic exceptions (such as when dividing by zero) pointer exceptions (such as trying to access an object through a null reference) indexing exceptions (such as attempting to access an array element through an index that is too large or too small) Runtime exceptions can occur anywhere in a program and in a typical program can be very numerous. Thus the compiler does not require that you catch or specify runtime exceptions, although you can. Checked exceptions are exceptions that are not runtime exceptions and are checked by the compiler; the compiler checks that these exceptions are caught or specified. If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Exceptions The finally block public void writeList() { PrintWriter out = null; try { out = new PrintWriter( new FileWriter("OutFile.txt")); for (int i = 0; i < size; i++) out.println("Value at: " + i + " = " + vector.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { // (3 possible versions of runtime – only one close) if (out != null) out.close(); } } }

Exceptions The finally block (2) If the JVM exits while the try or catch code is being executed, then the finally block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block will not execute even though the application as a whole continues.

Exceptions Specifying the Exceptions Thrown by a Method Sometimes, it's appropriate for your code to catch exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack handle the exception. To specify that method throws exceptions, you add a throws clause to the method signature. The throws clause is composed of the throws keyword followed by a comma-separated list of all the exceptions thrown by that method. The throws clause goes after the method name and argument list and before the curly bracket that defines the scope of the method. Example: public void writeList() throws IOException, ArrayIndexOutOfBoundsException {

Exceptions Choosing the exception type to throw You should write your own exception classes if you answer "yes" to any of the following questions. Otherwise, you can use java predefined exceptions: Do you need an exception type that isn't represented by those in the Java development environment? Would it help your users if they could differentiate your exceptions from those thrown by classes written by other vendors? Does your code throw more than one related exception? If you use someone else's exceptions, will your users have access to those exceptions? A similar question is: Should your package be independent and self-contained?

Exceptions Choosing the exception type to throw(2) Suppose you are writing a linked list class that you're planning to distribute as freeware. Among other methods, your linked list class supports these methods: objectAt(int n) -Returns the object in the n th position in the list. firstObject - Returns the first object in the list. indexOf(Object n) - Searches the list for the specified Object and returns its position in the list. Your structure of exceptions should be like this:

Exceptions Choosing the superclass Java exceptions must be Throwable objects (they must be instances of Throwable or a subclass of Throwable ).

Exceptions Choosing the superclass(2) Error s are reserved for serious hard errors that occur deep in the system. You shouldn't subclass RuntimeException unless your class really is a runtime exception. Generally you should subclass Exception. Naming Conventions It's good practice to append the word "Exception" to the end of all classes that inherit (directly or indirectly) from the Exception class. Similarly, classes that inherit from the Error class should end with the string "Error".

Exceptions Choosing the superclass(3) A method can detect and throw a RuntimeException when it's encountered an error in the virtual machine runtime. However, it's typically easier to just let the virtual machine detect and throw it. Normally, the methods you write should throw Exceptions, not RuntimeException. Similarly, you create a subclass of RuntimeException when you are creating an error in the virtual machine runtime (which you probably aren't). Otherwise you should subclass Exception. Do not throw a runtime exception or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

Exceptions Implementing Exceptions The simplest exception is: class SimpleException extends Exception { } But exceptions can be more complicated (and useful): class MyException extends Exception { private int i; public MyException() { } public MyException(String msg) { super(msg); } public MyException(String msg, int x) { super(msg); i = x; } public int val() { return i; } }

Exceptions Implementing Exceptions(2) Usage of MyException: public class ExtraFeatures { public static void f() throws MyException { System.out.println( "Throwing MyException from f()"); throw new MyException(); } public static void h() throws MyException { System.out.println( "Throwing MyException from h()"); throw new MyException( "Originated in h()", 47); } public static void main(String[] args) { try { f(); } catch(MyException e) { e.printStackTrace(System.err); } try { h(); } catch(MyException e) { e.printStackTrace(System.err); System.err.println("e.val() = " + e.val()); } } }

Exceptions Implementing Exceptions(3) The output is: Throwing MyException from f() MyException at ExtraFeatures.f(ExtraFeatures.java:22) at ExtraFeatures.main(ExtraFeatures.java:34) Throwing MyException from h() MyException: Originated in h() at ExtraFeatures.h(ExtraFeatures.java:30) at ExtraFeatures.main(ExtraFeatures.java:44) e.val() = 47

Exceptions Chained exceptions Methods and constructors in Throwable that support chained exceptions: Throwable getCause() Throwable initCause(Throwable) Throwable(String, Throwable) Throwable(Throwable) Example: try { } catch (IOException e) { throw new SampleException("Other IOException", e); }

Exceptions System.out and System.err System.err is better place to send error information than System.out, which may be redirected. If you send output to System.err it will not be redirected along with System.out so the user is more likely to notice it.

Exceptions Using Logging API (java.util.logging) try { Handler handler = new FileHandler("OutFile.log"); Logger.getLogger("").addHandler(handler); } catch (IOException e) { Logger logger = Logger.getLogger("package.name"); //name of the logger StackTraceElement elements[] = e.getStackTrace(); for (int i = 0, n = elements.length; i < n; i++) { logger.log(Level.WARNING, elements[i].getMethodName()); } Handler types: StreamHandler: => OutputStream. ConsoleHandler: => System.err FileHandler: => log files. SocketHandler: => remote TCP ports. MemoryHandler: => log records in memory

Exceptions Methods inherited from Throwable void printStackTrace( ) void printStackTrace(PrintStream) void printStackTrace(PrintWriter) Prints the Throwable and the Throwable’s call stack trace. The call stack shows the sequence of method calls that brought you to the point at which the exception was thrown. The first version prints to standard error, the second and third prints to a stream of your choiceprintStackTracePrintWriter

Exceptions Exceptions - questions Is there anything wrong with this exception handler as written? Will this code compile?... } catch (Exception e) {... } catch (ArithmeticException a) {... } Answer This first handler catches exceptions of type Exception; therefore, it catches any exception, including ArithmeticException. The second handler could never be reached. This code will not compile.

Exceptions Exceptions – questions(2) 1.Match each situation in the first column with an item in the second column. 1.error 2. checked exception 3. runtime exception 4. no exception a.int[] A; A[0] = 0; b.The Java VM starts running your program, but the VM can’t find the Java platform classes. (The Java platform classes reside in classes.zip or rt.jar.) c.A program is reading a stream and reaches the end of stream marker. d.Before closing the stream and after reaching the end of stream marker, a program tries to read the stream again. Answers: a-3 b-1 c-4 d-2