Exceptions and Exception Handling Carl Alphonce CSE116.

Slides:



Advertisements
Similar presentations
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Advertisements

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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
For use of Cleveland State's IST410 Students only 1 Exception.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
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.
1 CS2200 Software Development Lectures 28: Exception Handling A. O’Riordan, 2008 (Includes some slides by Lewis/Loftus 2005 and K. Brown )
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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
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.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Java Software Solutions Foundations of Program Design Sixth Edition
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.
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
Slides Credit Umair Javed LUMS Web Application Development.
Java Programming: Guided Learning with Early Objects
VB.Net - Exceptions Copyright © Martin Schray
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
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/
Exceptions Lecture 11 COMP 401, Fall /25/2014.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Java Exceptions a quick review….
Generics, Exceptions and Undo Command
Tirgul 13 Exceptions 1.
Chapter 13 Exception Handling
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Chapter 12 Exception Handling and Text IO
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Exception Handling.
Presentation transcript:

Exceptions and Exception Handling Carl Alphonce CSE116

Intermediate Java Topic overview  Exceptions and exception handling What are exceptions? When should you use exceptions? Details  How are exceptions generated (thrown)?  How are exceptions handled (caught)?  Runtime exceptions vs. other exceptions.

Intermediate Java What are exceptions?  Exceptions are a mechanism for handling “exceptional” situations which can occur at runtime.  Many languages provide exceptions.  Terminology: code where something unexpected happens “throws” an exception code which handles the exceptional situation “catches” the exception – this code is called an exception handler

Intermediate Java When are exceptions appropriate?  Exceptions are appropriate to use to signal to a caller a problematic situation which cannot be handled locally.  Example: Consider a file reading component which is used both by an interactive UI and a batch processing component. If the file reading component has a problem (e.g. “disk is full”), can it decide locally how to respond? No. It is up to the client to decide how to react. The UI may notify its human user. The batch processor may log the problem, and skip processing of this file.

Intermediate Java When are exceptions not appropriate?  It is not appropriate to use the exception mechanism when an exceptional situation can be handled locally.  It is not appropriate to use the exception mechanism in dealing with situations which are not exceptional. If a particular situation is expected, it should be explicitly checked for. For example, if a user supplies the name of a file to be read, it is better to check for existence of the file rather than to attempt to read and rely on a thrown exception to give notice if the file doesn’t exist.

Intermediate Java How are exceptions generated?  An exception is an object  An exception must derive from the java.lang.Exception class  Detour into inheritance and typing…

Intermediate Java Types and subtypes  Every class in Java defines a type.  Every interface in Java defines a type.  Types are arranged into a hierarchy: classes can extend classes; interfaces can extends interfaces; classes can implement interfaces.  Every class except Object has a parent class (which is Object if no other parent is given): every other class has exactly one parent.

Intermediate Java Hierarchy for Exceptions (partial)  Object Throwable  Error  LinkageError  ThreadDeath  VirtualMachineError  Exception  IOException  FileNotFoundException  MalformedURLException  RuntimeException  IndexOutOfBoundsException  NullPointerException

Intermediate Java Significance of hierarchy  The type hierarchy is significant, not only for exceptions, but for typing in Java more generally.  A variable declared to be of a given type can be assigned an object of that type, or of any subtype.  We make a distinction between the declared type of a variable, and the actual type of the object assigned to it.

Intermediate Java How are exceptions generated?  An exception is an object.  An exception must derive from the java.lang.Exception class.  An exception object must be instantiated from an exception class. new IndexOutOfBoundsException()  An exception must be thrown: throw new IndexOutOfBoundsException()

Intermediate Java What happens when an exception is thrown?  The exception is thrown until one of two things happens: an exception handler for the thrown exception is found, or the exception is uncaught (which typically results in program termination).  More technically, the runtime stack is unwound until a handler is found or the stack is empty.

Intermediate Java Runtime stack?  Every time a method is called, an invocation record is pushed onto the runtime stack.  An invocation record stores things like: parameter values local variables return value return location  When a method finishes, its corresponding invocation record is removed (“popped”) from the runtime stack.