SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Exceptions and Assertions.
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Exception Handling and Format output
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to –Improve the reliability.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
Chapter Chapter 8 Exceptions and Assertions.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
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.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
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. 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions and Assertions Animated Version.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
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.
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.
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.
Exception Handling and Format output. Midterm exam Date: Nov 1 st, 2006 Content: Week 1 to Week 8 Format: Multiple choice Determine the results of the.
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.
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.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
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 and Assertions Chapter 15 – CSCI 1302.
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.
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:
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.
DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Handling.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Java Exceptions a quick review….
Chapter 10 – Exception Handling
Exception Handling and Format output-CS1050-By Gayani Gupta
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Web Design & Development Lecture 7
Exception Handling Contents
Exceptions one last time…
Presentation transcript:

SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions

SE-1020 Dr. Mark L. Hornick 2 A few of 60+ exception classes

SE-1020 Dr. Mark L. Hornick 3 Catch or let go? The Error class represents serious problems that should not be caught by ordinary applications (e.g. thread death, out-of-memory). The Exception class represents error conditions that should be caught (e.g. divide-by-0, null reference, conversion errors).

SE-1020 Dr. Mark L. Hornick 4 Catching different types of Exceptions When there are multiple catch blocks in a try-catch statement, they are checked in sequence. It is important to check more specialized exception classes before the more general exception classes. When an exception is thrown, its matching catch block is executed and the other catch blocks are ignored.

SE-1020 Dr. Mark L. Hornick 5 Fumbling to the VM If none of the catch blocks matches the thrown exception, the system will search down the stack trace for a method with a matching catch block. If none is found, the system (VM) will handle the thrown exception And terminate your program after calling printStackTrace()

SE-1020 Dr. Mark L. Hornick 6 Getting information from exceptions There are two methods of the Throwable class that provide information about the thrown exception: getMessage – a String description printStackTrace – call stack dump

SE-1020 Dr. Mark L. Hornick 7 Catching Exceptions If there is a block of code that must be executed regardless of whether an exception is thrown, we use the reserved word finally: inputStr = JOptionPane.showInputDialog(null, “”); try{ number = Integer.parseInt(inputStr); return;// will not return until finally is processed } catch (NumberFormatException e) { System.out.println(“Cannot convert to int”); return; ;// will not return until finally is processed } finally { System.out.println(“DONE”); }

SE-1020 Dr. Mark L. Hornick 8 You can also throw exceptions An exception is thrown using the throw statement. throw where is an instance of the Throwable class or its subclasses. Exception is a subclass of Throwable By convention, we create Exceptions rather than Throwable objects

SE-1020 Dr. Mark L. Hornick 9 A few of 60+ exception classes Throwable is the root of all Exceptions and Errors Your program cannot catch or throw Errors Your program can catch or throw Exceptions

SE-1020 Dr. Mark L. Hornick 10 Throwing your own Exceptions inputStr = JOptionPane.showInputDialog(null, “Enter a value less than 100”); number = Integer.parseInt(inputStr); if (num>=100) { throw new Exception(“Out of bounds”); }

SE-1020 Dr. Mark L. Hornick 11 Passing the buck When a method may throw an exception, either directly or indirectly, the method is called an exception thrower. An exception thrower method can be a: Catcher – if it catches it Propagator – if it lets it go Can be both if it catches some types and lets others go

SE-1020 Dr. Mark L. Hornick 12 Catcher/Propagator Example inputStr = JOptionPane.showInputDialog(null, “Enter a value less than 100”); try{ number = Integer.parseInt(inputStr); if (num>=100) { throw new Exception(“Out of bounds”); } } catch (NumberFormatException e) { System.out.println(“Cannot convert to int”); } Here, NumberFormatExceptions are caught but Exceptions are propagated. The “Out of bounds” exception will not be caught here!

SE-1020 Dr. Mark L. Hornick 13 RuntimeException – a special class of Exception Some exceptions have to be caught by your code and not allowed to propagate to the VM A checked exception is an exception that is checked at compile time RuntimeException and classes that derive from it are unchecked. They are detected only at runtime

SE-1020 Dr. Mark L. Hornick 14 Only RuntimeExceptions can be left unchecked RuntimeExceptions and descendants are unchecked exceptions Exception and IOException are checked exceptions

SE-1020 Dr. Mark L. Hornick 15 Propagating Exceptions If a method is a checked exception propagator, its header must be modified to declare the type of exceptions the method propagates. Without the required throws clause, the program will not compile. The reserved word throws is used for this declaration. void SomeMethod( ) throws Exception { // some code that may cause an Exception // that is not caught here in the method... }

SE-1020 Dr. Mark L. Hornick 16 Callers of a method that can throw a checked exception must include the try-catch statement in the method body or the throws clause in the header.

SE-1020 Dr. Mark L. Hornick 17 Throws is optional for RuntimeExceptions For the exceptions of the type (or derived from the type) called RuntimeException, the throws clause is optional. If a method is a propagator of runtime exceptions or errors, the throws clause is optional.

SE-1020 Dr. Mark L. Hornick 18 It is optional for callers of a method that can throw runtime exceptions to include the try-catch statement in the method body or the throws clause in the header.