ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
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.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
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.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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 Briana B. Morrison CSE 1302C Spring 2010.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
COP INTERMEDIATE JAVA Exception Handling Serialization.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Handling Errors with Exception (in Java) Project 10 CSC 420.
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.
Exceptions COMPSCI 105 S Principles of Computer Science.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
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.
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.
1 Advanced Flow of Control - Introduction - zTwo additional mechanisms for controlling process execution are exceptions and threads zChapter 14 focuses.
Exception Handling. Definition  The term exception is shorthand for the phrase "exceptional event.“  An exception is an event, which occurs during the.
Object Oriented Programming
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.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
© 2004 Pearson Addison-Wesley. All rights reserved November 30, 2007 Exceptions ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
CSE 1201 Object Oriented Programming
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Java Basics Exception Handling.
Presentation transcript:

ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions

ECE122 L23: Exceptions December 6, 2007 Overview °Problem: Can we detect run-time errors and take corrective action? °Try-catch Test for a variety of different program situations °Java provides a series of test cases °Allows for graceful exit of program or treatment of special cases

ECE122 L23: Exceptions December 6, 2007 Errors °Errors do occur in programming. Problems opening a file, dividing by zero, accessing an out-of-bounds array element, hardware errors, and many more °The question becomes: What do we do when an error occurs? How is the error handled? Who handles it? Should the program terminate? Can the program recover from the error? Should it? °Java uses exceptions to provide the error-handling capabilities for its programs.

ECE122 L23: Exceptions December 6, 2007 Exceptions °An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Represented as an object in Java °Throwing an exception An error occurs within a method. An exception object is created and handed off to the runtime system. The runtime system must find the code to handle the error. °Catching an exception The system searches for code to handle the thrown exception. It can be in the same method or in some method in the call stack.

ECE122 L23: Exceptions December 6, 2007 Exceptions °An exception is an object that describes an unusual or erroneous situation °Exceptions are thrown by a program May be caught and handled by another part of the program °A program can be separated into a normal execution flow and an exception execution flow °An error is also represented as an object in Java Usually represents a unrecoverable situation and should not be caught

ECE122 L23: Exceptions December 6, 2007 Handling Exceptions °General form: try { statement(s); } catch (ExceptionType name) { statement(s); } finally { statement(s); }

ECE122 L23: Exceptions December 6, 2007 Exception Handling °Java has a predefined set of exceptions and errors that can occur during execution °A program can deal with an exception in one of three ways: ignore it handle it where it occurs handle it an another place in the program °The manner in which an exception is processed is an important design consideration

ECE122 L23: Exceptions December 6, 2007 Exception Handling °If exception is ignored by the program The program will terminate abnormally and produce an appropriate message °The message includes a call stack trace that: indicates the line on which the exception occurred shows the method call trail that lead to the attempted execution of the offending line

ECE122 L23: Exceptions December 6, 2007 Exception Class Hierarchy °Java has a predefined set of exceptions and errors that can occur during execution. Exception (derived from Throwable) ClassNotFoundException ArithmeticException IndexOutOfBoundsException NullPointerException IOExceptionRunTimeException This is just a small part of the Exception hierarchy NumberFormatException

ECE122 L23: Exceptions December 6, 2007 The try Statement °Handling an exception in a program The line that throws the exception is executed within a try block °A try block is followed by one or more catch clauses °Each catch clause has an associated exception type and is called an exception handler °When exception occurs: Processing continues at the first catch clause that matches the exception type

ECE122 L23: Exceptions December 6, 2007 Handling Exceptions °Three statements help define how exceptions are handled: try- identifies a block of statements within which an exception might be thrown catch - must be associated with a try statement and identifies a block of statements that can handle a particular type of exception. -The statements are executed if an exception of a particular type occurs within the try block. -A try statement can have multiple catch statements associated with it. finally - must be associated with a try statement - identifies a block of statements that are executed regardless of whether or not an error occurs within the try block. -Even if the try and catch block have a return statement in them, finally will still run.

ECE122 L23: Exceptions December 6, 2007 The finally Clause °A try statement can have an optional clause following the catch clauses Designated by the reserved word finally °The statements in the finally clause always are executed °If no exception is generated: The statements in the finally clause are executed after the statements in the try block complete °If an exception is generated: The statements in the finally clause are executed after the statements in the appropriate catch clause complete

ECE122 L23: Exceptions December 6, 2007 Summary °Exceptions form an important part of Java °Graceful error checking and handling allow for better designed code °Try-catch evaluates a number of possibilities °Often error messages are the result