Exceptions CIS 304 Intermediate Java Programming for Business.

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.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
COMP 121 Week 5: Exceptions and Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
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:
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
For use of Cleveland State's IST410 Students only 1 Exception.
 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.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
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.
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.
Applets & Applications CSC 171 FALL 2001 LECTURE 15.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CIS 270—Application Development II Chapter 13—Exception Handling.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Slides Credit Umair Javed LUMS Web Application Development.
Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.
Java Programming: Guided Learning with Early Objects
COMP Exception Handling Yi Hong June 10, 2015.
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.
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.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
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.
Copyright © Curt Hill Error Handling in Java Throwing and catching 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.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
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.
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 and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Chapter 14 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Chapter 12 Exception Handling
Web Design & Development Lecture 7
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Chapter 12 Exception Handling and Text IO Part 1
Java Basics Exception Handling.
Presentation transcript:

Exceptions CIS 304 Intermediate Java Programming for Business

Guarding Your Statements 4 When you anticipate a problem you can write code that will keep the problem from happening 4 But, can't always do that, because –Don't know what the problem will be –Don't know how to handle it –Will discover the problem too late

Basic Approach 4 Use two blocks of code –try block This block contains the code that may cause the exception –catch block This block contains the code that deals with the exception –May "fix" the error –May write a message to the user –May terminate the program, with or without a message

Example try {price = Double.parseDouble(priceStr);} catch (NumberFormatException nfe){ JOptionPane.showMessageDialog( null, "price must be numeric – " + "reenter and hit 'Submit'"); }

Multiple Errors 4 Sometimes more than one type of error may be anticipated. If so, –Have several "catch" blocks –Include these in decreasing order of specificity because only the first applicable catch block executes You want to know as exactly as possible what error occurred, so you can deal with it precisely If you do the more general first, then you can't get to the specific The compiler may give you an error saying you can't get to the code in the more specific catch statement

The finally block 4 You can include code that will be executed regardless of what happens in the try or the catch blocks try {…code that may throw an exception…} catch (…) {…code to deal with exception…} finally {…code that executes after above…}

"Popular" Exceptions 4 NumberFormatException 4 NullPointerException 4 ArithmeticException 4 ClassCastException 4 ArrayIndexOutOfBoundsException 4 RuntimeException

Two Methods to use with Exceptions 4 Example: catch (IOException ioe) { system.out.println(ioe.printStackTrace()); system.out.println(ioe.getMessage()); }

Other Possibilities 4 Instead of catch have method "throw an exception" public double myMethod(int myInt) throws NumberFormatException {…code of method that may generate NumberFormatException…no catch here} ********************************* And then in the code where myMethod is called try {myObject.myMethod(5)} catch (NumberFormatException nfe) {… deal with exception here, such as with error message…)}

Throwing Your Own Exceptions 4 Your program logic may be able to detect errors that would cause exceptions 4 Rather than letting the JVM throw the error, your program can do it itself 4 To do this your code would include, for example throw new NumberFormatException("Value entered was not numeric"); 4 Advantages –You control the timing –You get to write your own error message, which is the String argument in the constructor above

Checked Exceptions 4 "Checked Exceptions" are exceptions that Java knows may occur –Java insists that the program be prepared for Checked Exceptions and that these be caught 4 If the compiler knows that an error may result from an operation, it will insist that the exception be caught, or be declared to be thrown –Failure to do this will cause a compiler error 4 IOException and its subclasses are checked exceptions

In-Class Exercise 1. Write a simple application that will ask the user to enter a location and name of a simple.txt file that contains just one record, and open that file. If the file does not exist, then prompt the user to reenter the name and location again. 2. Write a simple GUI application that will ask the user to enter a number of employees, and the incentive pay pool. The pool is shared evenly. Calculate and display the input values and the per employee payout from the pool. –Make sure that both entries are numeric –Make sure that you handle division by zero

In-Class Exercise (continued) 3.Instead of using try / catch in 2 above, catch the exception in the calling method (and not in the method itself where the exception is detected)