COP 2800 Lake Sumter State College Mark Wilson, Instructor.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Yoshi
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
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.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
COMP 121 Week 5: Exceptions and Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
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.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
COP INTERMEDIATE JAVA Exception Handling Serialization.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
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 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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Handling Errors with Exception (in Java) Project 10 CSC 420.
Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Advanced Java Programming and Database connection Eran.
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
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling. Definition  The term exception is shorthand for the phrase "exceptional event.“  An exception is an event, which occurs during the.
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.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
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.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
Practice Session 9 Exchanger CyclicBarrier Exceptions.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
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.
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.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
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.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Java Exceptions a quick review….
Introduction to Exceptions in Java
Advanced Programming Behnam Hatami Fall 2017.
ATS Application Programming: Java Programming
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
Java Exceptions Dan Fleck CS211.
Java Basics Exception Handling.
Exception Handling.
Presentation transcript:

COP 2800 Lake Sumter State College Mark Wilson, Instructor

Exceptions

 Methods with unknown side effects Versioning Latent bug  Methods that may not work at runtime Unavailable system resources Abnormal or unexpected response

 An event that occurs during the execution of a program that disrupts the normal flow of instructions  Method Java uses to handle errors and other exceptional events  An exception object, contains information about the error, including its type and the state of the program when the error occurred  Creating an exception object and handing it to the runtime system is called throwing an exception.

 Traditional processing breaks well formed constructs, makes spaghetti code  The runtime system searches the call stack for a method that contains a block of code that can handle the exception (exception handler)  An exception handler is considered appropriate if the type of the exception object thrown matches the type that can be handled by the handler  Uncaught exceptions cause the runtime system (and, therefore, the program) to terminate

 Separates Error-Handling Code from "Regular" Code In traditional programming, error detection, reporting, and handling often lead to confusing spaghetti code  Propagating Errors Up the Call Stack Java runtime environment searches backward through the call stack to find any methods that are interested in handling a particular exception  Grouping and Differentiating Error Types grouping or categorizing of exceptions is a natural outcome of the class hierarchy

 Checked Exception – Exception class well-written application anticipates and recovers  Error – Error class external to the application application usually cannot anticipate or recover  Runtime Exception – RuntimeException class internal to the application application usually cannot anticipate or recover  Error and runtime exceptions are unchecked exceptions

 throws Method declares it may throw an exception  try Sets off the code block for risky behavior  catch Sets off code to handle an exception Follows try Multiple catches possible  finally Always executes after the try block May not execute if the JVM exits during a try or catch Good place to put cleanup code (e.g. close open files)

public void writeList() { PrintWriter out = null; try { System.out.println("Entering" + " try statement"); out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) out.println("Value at: " + i + " = " + vector.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); }

 Methods that don’t catch checked exceptions must specify that they can throw exceptions  Identifies exceptions that may need to be caught  Unchecked exceptions are optional public void writeList() throws IOException, ArrayIndexOutOfBoundsException { public void writeList() throws IOException {

 Any code can throw exceptions  Exceptions are thrown with throw  All exceptions are descendants of Throwable  Most programs throw and catch objects derived from the Exception class public Object pop() { Object obj; if (size == 0) { throw new EmptyStackException(); } obj = objectAt(size - 1); setObjectAt(size - 1, null); size--; return obj; }

 Exception type isn't represented by those in the Java platform?  Help users differentiate your exceptions from other vendors?  Does your code throw more than one related exception?  Will users have access to other’s exceptions?  Most applications will throw objects that are Exceptions. Errors are normally used for serious, hard errors in the system  For readable code, it's good practice to append the string Exception to the names of all classes that inherit (directly or indirectly) from the Exception class.

 A program can use exceptions to indicate that an error occurred.  To throw an exception, use the throw statement and provide it with an exception object — a descendant of Throwable — to provide information about the specific error that occurred.  A method that throws an uncaught, checked exception must include a throws clause in its declaration.  A program can catch exceptions by using a combination of the try, catch, and finally blocks.  The try block identifies a block of code in which an exception can occur.  The catch block identifies a block of code, known as an exception handler  The finally block identifies a block of code that is guaranteed to execute,  The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.  The class of the exception object indicates the type of exception thrown.  The exception object can contain further information about the error  With exception chaining, an exception can point to the exception that caused it

Packages, Jars and Deployment. Oh, my!

Beginning GUI