Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: 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.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
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:
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
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.
CS1101: Programming Methodology Aaron Tan.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
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.
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.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Java Programming: Guided Learning with Early Objects
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
VB.Net - Exceptions Copyright © Martin Schray
COMP Exception Handling Yi Hong June 10, 2015.
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.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
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.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
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.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Chapter 10 – Exception Handling
Testing and Exceptions
Chapter 14: Exception Handling
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Lecture 11 Objectives Learn what an exception is.
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.
Chapter 12 Exception Handling and Text IO Part 1
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel

Java Programming: From Problem Analysis to Program Design, 4e 2 Exception Definition: an occurrence of an undesirable situation that can be detected during program execution Examples  Division by zero  Invalid input errors  An array index that goes out of bounds

Java Programming: From Problem Analysis to Program Design, 4e 3 Handling Exception within a Program Can use an if statement to handle an exception However, suppose that division by zero occurs in more than one place within the same block  In this case, using if statements may not be the most effective way to handle the exception

Java Programming: From Problem Analysis to Program Design, 4e 4 Java’s Mechanism of Exception Handling Java provides a number of exception classes to effectively handle certain common exceptions such as division by zero, invalid input, and file not found When an exception occurs, an object of a particular exception class is created

Java Programming: From Problem Analysis to Program Design, 4e 5 When a division by zero exception occurs, the program creates an object of the class ArithmeticException When a Scanner object is used to input data into a program, any invalid input errors are handled using the class InputMismatchException The class Exception is the superclass of all the exception classes in Java Java’s Mechanism of Exception Handling

Java Programming: From Problem Analysis to Program Design, 4e 6 Java Exception Hierarchy

Java Programming: From Problem Analysis to Program Design, 4e 7 Java Exception Hierarchy

Java Programming: From Problem Analysis to Program Design, 4e 8 Java Exception Hierarchy

Java Programming: From Problem Analysis to Program Design, 4e 9 try / catch / finally Block Statements that might generate an exception are placed in a try block The try block might also contain statements that should not be executed if an exception occurs The try block is followed by zero or more catch blocks A catch block specifies the type of exception it can catch and contains an exception handler

Java Programming: From Problem Analysis to Program Design, 4e 10 try / catch / finally Block The last catch block may or may not be followed by a finally block Any code contained in a finally block always executes, regardless of whether an exception occurs, except when the program exits early from a try block by calling the method System.exit If a try block has no catch block, then it must have the finally block

Java Programming: From Problem Analysis to Program Design, 4e 11 try / catch / finally Block

Java Programming: From Problem Analysis to Program Design, 4e 12 try / catch / finally Block (continued) If no exception is thrown in a try block, all catch blocks associated with the try block are ignored and program execution resumes after the last catch block If an exception is thrown in a try block, the remaining statements in the try block are ignored - The program searches the catch blocks in the order in which they appear after the try block and looks for an appropriate exception handler

Java Programming: From Problem Analysis to Program Design, 4e 13 try / catch / finally Block If the type of the thrown exception matches the parameter type in one of the catch blocks, the code of that catch block executes and the remaining catch blocks after this catch block are ignored If there is a finally block after the last catch block, the finally block executes regardless of whether an exception occurs

Java Programming: From Problem Analysis to Program Design, 4e 14 Order of catch Blocks The heading of a catch block specifies the type of exception it handles. If in the heading of a catch block you declare an exception using the class Exception, then that catch block can catch all types of exceptions because the class Exception is the superclass of all exception classes

Java Programming: From Problem Analysis to Program Design, 4e 15 Exception-Handling Techniques Terminate program  Output appropriate error message upon termination. Log error and continue  Display error messages and continue with program execution Fix error and continue  Repeatedly get user input  Output appropriate error message until valid value is entered

Java Programming: From Problem Analysis to Program Design, 4e 16 Unchecked Exceptions Syntax: ExceptionType1, ExceptionType2, and so on are names of exception classes

Java Programming: From Problem Analysis to Program Design, 4e 17 Exceptions Example Code public static void exceptionMethod() throws InputMismatchException,FileNotFoundException { //statements } The method exceptionMethod throws exceptions of the type InputMismatchException and FileNotFoundException

Java Programming: From Problem Analysis to Program Design, 4e 18 Rethrowing and Throwing an Exception When an exception occurs in a try block, control immediately passes to one of the catch blocks; typically, a catch block does one of the following:  Completely handles the exception.  Rethrows the same exception for the calling environment to handle the exception.  Partially processes the exception; in this case, the catch block either rethrows the same exception or throws another exception for the calling environment to handle the exception

Java Programming: From Problem Analysis to Program Design, 4e 19 Rethrowing and Throwing an Exception Syntax throw exceptionReference;

Java Programming: From Problem Analysis to Program Design, 4e 20 import java.util.*; public class RethrowExceptionExmp1 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int number; try { number = getNumber(); System.out.println("Line 5: number = ”+ number); } catch (InputMismatchException ex) { System.out.println("Line 7: Exception ” + ex.toString()); } Rethrowing and Throwing an Exception

Java Programming: From Problem Analysis to Program Design, 4e 21 public static int getNumber()throws InputMismatchException { int num; try { System.out.print(“Line 11: Enter an integer: "); num = console.nextInt(); System.out.println(); return num; } catch (InputMismatchException e) { throw e; } Rethrowing and Throwing an Exception

Trace By Nora Alaqeel