Chapter 12 Exception Handling

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

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
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.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
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:
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 12 Exception Handling and Text.
1 Chapter 18 Exception Handling. 2 Motivations F Program runs into a runtime error –program terminates abnormally F How can you handle the runtime error.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Exception Handling.
Exception Handling. Definition  The term exception is shorthand for the phrase "exceptional event.“  An exception is an event, which occurs during the.
CIS 270—Application Development II Chapter 13—Exception Handling.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
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.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
CS 112 Programming 2 Lecture 08 Exception Handling & Text I/O (1)
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:
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Chapter 12 Exceptions and File Input/Output
Java Exceptions a quick review….
Chapter 13 Exception Handling
Chapter 12 Exception Handling And Text IO
Chapter 13 Exception Handling
Exception Handling in Java Reference: COS240 Syllabus
Topic: Exception Handling
Chapter 12 Exception Handling and Text IO
Chapter 7 Exceptions and Assertions
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Exception Handling Chapter 9.
Chapter 12 Exception Handling and Text IO
Chapter 11 Exception Handling and Text I/O
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Chapter 12 Exception Handling and Text IO
Web Design & Development Lecture 7
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 13 Exception Handling
Chapter 12: Exceptions and Advanced File I/O
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO Part 1
Chapter 12 Exception Handling and Text IO
Exceptions 10-May-19.
Chapter 12 Exception Handling and Text IO
Java Basics Exception Handling.
Exception Handling.
Presentation transcript:

Chapter 12 Exception Handling You have learned that: a runtime error is also called an exception You have learned that: an exception is an actual object that represents an error or condition. You have learned that: Exceptions are thrown from a method. You have learned that: the caller of the method can catch and handle the exception. You had the opportunity to create a custom exception handler. What’s new ?: there are “canned” or “pre-existing” exceptions What’s new ?: those “pre-existing” exceptions are objects, and objects are defined using classes What’s new ?: the root class for those “pre-existing” exceptions is java.lang.Throwable Dr. Clincy ------ Lecture

Exception Types There are many predefined exception classes in the Java API Classified into three major types: (1) System Errors, (2) Exceptions, and (3) Runtime Exceptions . Btw, all are “runtime” errors Dr. Clincy ------ Lecture

System Errors System errors are thrown by the JVM and are represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully. Two classes are dependent on one another and one of them is no longer compatible after a compilation The JVM is broken or has run out of the needed resources to operate Dr. Clincy ------ Lecture

Exceptions Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program. Attempt to use a class that doesn’t exist Invalid input, reading past end of file, and opening a nonexistent file Dr. Clincy ------ Lecture

Exceptions continuing … Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program. Dividing integer by zero. Programming type errors Attempting to access an object through a null reference variable Index to an array out of bound Method passing an argument that is illegal or inappropriate Dr. Clincy ------ Lecture

Checked Exceptions vs. Unchecked Exceptions RuntimeException, Error and their subclasses are known as unchecked exceptions – meaning that the compiler does NOT force the programmer to deal with the exception. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions. In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. Unchecked exceptions can occur anywhere in the program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate you to write code to catch unchecked exceptions. Dr. Clincy ------ Lecture

Declaring, Throwing, and Catching Exceptions We already know that Java’s exception-handling model is based on three operations: (1) declaring an exception, (2) throwing an exception, and (3) catching an exception Declaring Exceptions Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. Should be declared as follows: public void myMethod() throws IOException public void myMethod() throws Exception1, …,ExceptionN Just one exception Multiple exceptions NOTE: if a method does not declare exceptions in the superclass, you cannot override it to declare exceptions in the subclass. Dr. Clincy ------ Lecture

Throwing Exceptions Example When the program detects an error, the program can create an instance of an appropriate exception type and throw it. This is known as throwing an exception. Should be declared as follows: throw new TheException(); Throwing Exceptions Example /** Set a new radius */ public void setRadius(double newRadius)throws IllegalArgumentException { if (newRadius >= 0) radius = newRadius; else throw new IllegalArgumentException("Radius cannot be negative"); } Declaring the exception at the beginning of the Method If error is detected – this code creates an instance of the exception and throws it. Dr. Clincy ------ Lecture

Catching Multiple Exceptions You know how to declare multiple exceptions … and you also know how to throw multiple exceptions. Multiple exceptions can be caught and handled in a try-catch bock. For example: try { statements; // Statements that may throw exceptions } catch (Exception1 exVar1) { handler for exception1; catch (Exception2 exVar2) { handler for exception2; ... catch (ExceptionN exVar3) { handler for exceptionN; NOTE: if no exceptions arise during execution of the try block, the catch blocks are skipped. Dr. Clincy ------ Lecture

Getting Info from Exceptions Exception object contains valuable info about the exception. The following methods may be used: Great for debugging runtime errors Provides step-by-step access to trace stack info printed by printStackTrace() Dr. Clincy ------ Lecture

The finally Clause try { statements; } catch(TheException ex) { Sometimes you will want some code to be executed regardless of whether an exception occurs or not. The finally clause can be used to accomplish this. try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Dr. Clincy ------ Lecture

Let’s cover Lab 7 now Dr. Clincy ------ Lecture