Exceptions and Exception Handling (1) Carl Alphonce CSE116.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

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.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
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.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exceptions and Exception Handling Carl Alphonce CSE116.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
COP INTERMEDIATE JAVA Exception Handling Serialization.
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.
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.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
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
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.
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
COMP Exception Handling Yi Hong June 10, 2015.
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.
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.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
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.
Object Throwable ErrorException RuntimeException.
Java Exceptions a quick review….
Tirgul 13 Exceptions 1.
Chapter 13 Exception Handling
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
ATS Application Programming: Java Programming
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Exception Handling and Reading / Writing Files
Web Design & Development Lecture 7
Chapter 13 Exception Handling
Java Basics Exception Handling.
Presentation transcript:

Exceptions and Exception Handling (1) 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()