BIO Java 1 Exception Handling Aborting program not always a good idea – E-mail: can’t lose messages – E-commerce: must ensure correct handling of private.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Index Exception handling Exception In Java Exception Types
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
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.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
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.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
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.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
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.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
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.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Java Exceptions a quick review….
C++ Exceptions.
ATS Application Programming: Java Programming
Web Design & Development Lecture 7
Chapter 13 Exception Handling
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.
Exception Handling.
Presentation transcript:

BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private info in case of crash – Antilock braking, air-traffic control: must recover and keep working Java includes mechanisms for recovering from exceptions

BIO Java 2 Exceptions in Java Exceptions are (special) objects in Java They are created from classes The classes are derived from a special class, Throwable

BIO Java 3 Java Exception Hierarchy Throwable The base class for all exceptions, it is required for a class to be the rvalue to a throw statement. Error Any exception so severe it should be allowed to pass uncaught to the Java runtime. Exception Anything which should be handled by the invoker is of this type, and all but five exceptions are.

BIO Java 4 Java Exception Terminology When an anomaly is detected during program execution, the JVM throws an exception – There are built-in exceptions – Users can also define their own To avoid crashing, a program can catch a thrown exception An exception generated by a piece of code can only be caught if the program is alerted. This process is called trying the piece of code

BIO Java 5 Exception Handling When an error occurs in a Java method, the method creates an object of type Exception and gives it to the run-time system. The Exception object consists of the information pertaining to the exception, like its type and the state of the program when the exception occurred.

BIO Java 6 Exception Handling The run-time system looks for the appropriate method that would handle the error occurred. The process of creating an Exception object and handling it to the run-time system is called throwing an exception.

BIO Java 7 Exception Handling The method that handles the exception thrown is called an exception handler. The exception handler is used to catch the exception thrown.

BIO Java 8 Exception Handling The run-time system searches for the exception handler, starting from the method where the error occurred, the search goes on…till an appropriate handler is found. If not found, then the run-time system terminates the application.

BIO Java 9 9 Keywords for Java Exceptions throws Describes the exceptions which can be raised by a method. throw Raises an exception to the first available handler in the call stack, unwinding the stack along the way. try Marks the start of a block associated with a set of exception handlers. catch If the block enclosed by the try generates an exception of this type, control moves here; watch out for implicit subsumption. finally Always called when the try block concludes, and after any necessary catch handler is complete.

BIO Java 10 try-catch block Program statements that we want to monitor for exceptions are contained within a try block. If an exception occurs with-in the try block, it is thrown. Our code can catch this exception and handle it in some rational manner.

BIO Java 11 An Example …. try { int a=0; int b=5; System.out.println("Div=" + (b/a)); } catch (ArithmeticException e) { //code to handle }

BIO Java 12 throw, throws, finally System generated exceptions are automatically thrown by the JRE. To manually throw an exception, use the keyword throw. An exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block

BIO Java 13 throw, throws, finally System generated exceptions are automatically thrown by the JRE. To manually throw an exception, use the keyword throw. An exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block

BIO Java 14 Checked & Unchecked Exceptions Unchecked exceptions – any exceptions that is derived from class Error and class RunTimeException. All other exceptions are called checked exceptions. The compiler checks that exception handlers are there for all checked exceptions.

BIO Java 15 Checked Exceptions Java method can throw an exception if it encounters a situation it cant handle. A method not only declares its signature but it also tells the compiler what can go wrong. (the exceptions which it may throw). throws clause at the header part of the method states the exceptions which it throws. Eg. void m(int a) throws IOException

BIO Java 16 Throwing an Exception The keyword throw is used to throw an exception from a method. Find an appropriate exception class. Make an object of that class. Throw it.

BIO Java 17 finally finally clause – creates a block of code that will be executed after a try/catch block is completed and before the code following the try/catch block.