Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.

Slides:



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

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
© 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,
TCU CoSc Programming with Java Handling Exceptions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
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.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
1 Advanced Flow of Control - Introduction - zTwo additional mechanisms for controlling process execution are exceptions and threads zChapter 14 focuses.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
Slides Credit Umair Javed LUMS Web Application Development.
10-1 Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled.
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.
Chapter 5: Enhancing Classes
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.
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.
© 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.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Chapter 10 Exceptions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 1201 Object Oriented Programming
16 Exception Handling.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Chapter 10 – Exception Handling
Chapter 13 Exception Handling
10 Exceptions Software Solutions Lewis & Loftus java 5TH EDITION
Introduction to Exceptions in Java
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling Chapter 9.
CMSC 202 Exceptions.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Abdulmotaleb El Saddik University of Ottawa
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Chapter 5: Enhancing Classes
Part B – Structured Exception Handling
Exception Handling in Java
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exceptions April 19, 2006 ComS 207: Programming I (in Java)
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exceptions 10-May-19.
Lecture 9.
Chapter 11: Exception Handling
Exception Handling and Event Handling
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines a problem that can usually be fixed.

Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and 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, but usually represents a unrecoverable situation and should not be caught

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

Exception Handling If an 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 The call stack trace also shows the method call trail that lead to the attempted execution of the offending line Read after the slide: Handling exceptions is not required on the AP test. However, you should be familiar with common exceptions and throwing exceptions.

throw new NoSuchElementException(); The throw Statement Exceptions are thrown using the throw statement Usually a throw statement is nested inside an if statement that evaluates the condition to see if the exception should be thrown The statement at the bottom of the slide throws a NoSuchElementException: These exceptions are known as user-defined exceptions. In order to throw user defined exceptions, the throw keyword is being used. The purpose of the exception is to handle code that doesn’t behave in the way that is expected. throw new NoSuchElementException();

Here is an example of a thrown exception with a try / catch block to handle the exception. In this example, we are catching invalid input from the user. The user will not see the exception string literal text “Age can’t be less than zero” but the string literal will assist the programmer in determining the reason the code did not work as intended.