Exceptions and Assertions Chapter 15 – CSCI 1302.

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

Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
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:
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
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.
Java Software Solutions Foundations of Program Design Sixth Edition
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Exception Handling.
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.
Chapter 12: Exception Handling
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Exception Handling and Text.
Introduction to Exception Handling and Defensive Programming.
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.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
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.
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.
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.
Exceptions in the Java programming language J. W. Rider.
Java Exceptions a quick review….
Chapter 13 Exception Handling
Chapter 13 Exception Handling
Exception Handling in Java Reference: COS240 Syllabus
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Chapter 11 Exception Handling and Text I/O
Exception Handling Chapter 9 Edited by JJ.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 13 Exception Handling
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO Part 1
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Exception Handling.
Presentation transcript:

Exceptions and Assertions Chapter 15 – CSCI 1302

CSCI 1302 – Exceptions and Assertions2 Outline Introduction Exceptions and Exception Types –Exception Classes –Checked and Unchecked Exceptions Understanding Exception Handling –Declaring Exceptions –Throwing Exceptions –Catching Exceptions Rethrowing Exceptions

CSCI 1302 – Exceptions and Assertions3 Outline The finally clause When to Use Exceptions Creating Custom Exception Classes Assertions –Declaring Assertions –Running Programs with Assertions –Using Exception Handling or Assertions

CSCI 1302 – Exceptions and Assertions4 Introduction Three types of errors: –Syntax errors –Runtime errors –Logic errors Exception handling deals with runtime errors Assertions promote program correctness

CSCI 1302 – Exceptions and Assertions5 Exceptions and Exception Types Events that occur during the execution of a program and disrupt the normal flow of control Abnormal termination can lead to many serious issues Exceptions occur whenever Java detects a runtime error Unlike events, exceptions cannot be ignored Java provides exception handling

CSCI 1302 – Exceptions and Assertions6 Exceptions and Exception Types Consider a program that only takes integer input Typically, entering anything other than an integer will cause the program to terminate abnormally (See Test.java ) Using a try-catch block, we can detect this and respond to it (See TestException.java, TestExceptionRobust.java )

CSCI 1302 – Exceptions and Assertions7 Exception Classes

CSCI 1302 – Exceptions and Assertions8 Exception Classes All exceptions are instances of a class derived from Throwable Three major types of exceptions: –System errors – Internal system errors thrown by JVM, very rare –Exceptions – Errors caused by your program and external circumstances –Runtime exceptions – Programming errors

CSCI 1302 – Exceptions and Assertions9 Checked/Unchecked Exceptions RuntimeException, Error, and their subclasses are unchecked exceptions All others are checked, meaning the compiler forces the programmer to check for them and deal with them Typically, an unchecked exception is an indication of a programming logic error These can occur anywhere, which is why Java doesn’t force you to check them

CSCI 1302 – Exceptions and Assertions10 Understanding Exception Handling The exception handling model is based on three items –Declaring an exception –Throwing an exception –Catching an exception

CSCI 1302 – Exceptions and Assertions11 Declaring Exceptions Every method must state the types of checked exceptions it might throw To declare an exception, a method should use the throws keyword public void rfile() throws IOException Methods can throw multiple exceptions public void rfile() throws IOException, IllegalArgumentException, Exception3, Exception4

CSCI 1302 – Exceptions and Assertions12 Throwing Exceptions Create an instance of the appropriate exception and throw it IllegalArgumentException ex = new IllegalArgumentException(“Wrong Argument”); throw ex; or as an alternative throw new IllegalArgumentException(“Wrong Argument”);

CSCI 1302 – Exceptions and Assertions13 Catching Exceptions If no exceptions occur during a try-catch block, the catch blocks are not executed Once an exception occurs, the remaining lines of code in the try block are ignored and control goes to the catch blocks The exception handler is the code that handles the particular exception

CSCI 1302 – Exceptions and Assertions14 Catching Exceptions Java looks through the catch blocks from first to last If no handler is found, the exception is passed to the invoking method If no handler is found after moving all the way up the invocation chain, an error is printed to the console Catching an exception describes this process of finding a handler

CSCI 1302 – Exceptions and Assertions15 Catching Exceptions See text on pg. 555

CSCI 1302 – Exceptions and Assertions16 Catching Exceptions If a handler is found, the exception object is assigned to that variable These objects contain useful information that can be retrieved – getMessage() – Detailed message – toString() –Full name of exception class: getMessage() – printStackTrace() – Throwable object and stack info See TestCircleWithException.java

CSCI 1302 – Exceptions and Assertions17 Catching Exceptions Methods are executed on threads Exceptions terminate the thread they are being run on GUI applications use many threads while running Can throw an exception and not terminate See IntegerDivision.java See IntegerDivisionException.java

CSCI 1302 – Exceptions and Assertions18 Rethrowing Exceptions Exceptions can be rethrown to allow any necessary actions to be completed try { statements; } catch (Exception ex) { pre-exit operations; throw ex; }

CSCI 1302 – Exceptions and Assertions19 The finally Clause The finally clause is code executed regardless of an exception occurring or not try { statements; } catch (Exception ex) { handle ex; } finally { final statements; } See pg. 561 for examples

CSCI 1302 – Exceptions and Assertions20 When to use Exceptions Exception handling provides a robust solution to handle errors Using it can be expensive however, it instantiates new objects, rolls back the call stack, and sometimes has to travel the chain of invoking methods If an exception can be handled inside a method, there is no reason to throw it If an error occurs in multiple places, consider creating an exception class

CSCI 1302 – Exceptions and Assertions21 When to Use Exceptions Simple errors in individual methods shouldn’t be handled with exceptions Try-catch blocks should be used to handle unexpected error conditions try { System.out.println(rVar); } catch (NullPointerException ex) { System.out.println(“rVar is null”); }

CSCI 1302 – Exceptions and Assertions22 When to Use Exceptions The previous code should be replaced with something similar to: if (rVar != null) System.out.println(rVar); else System.out.println(“rVar is null”);

CSCI 1302 – Exceptions and Assertions23 Custom Exception Classes Java provides many exception classes that should be used whenever possible Occasionally, you will need to create an exception that cannot be handled by the built- in exception classes Create your own class and have it descend from Exception or any of its subclasses See RadiusException.java and CircleWithException2.java

CSCI 1302 – Exceptions and Assertions24 Assertions A Java statements that enables you to assert an assumption about your program Contains a Boolean expression that should be true during execution Used to ensure program correctness and avoid logic errors

CSCI 1302 – Exceptions and Assertions25 Declaring Assertions Use the keyword assert assert assertion; or assert assertion: detailedMessage; If an assertion is false, an AssertionError is thrown, the program displays a message on the console and exits See AssertionDemo.java

CSCI 1302 – Exceptions and Assertions26 Running Programs with Assertions By default, assertions are disabled at runtime Enable them with the –enableassertions or –ea switch on the command line java –ea AssertionDemo

CSCI 1302 – Exceptions and Assertions27 Exception Handling or Assertions Assertions address correctness Exceptions address robustness Assertions should not be used to replace exceptions Do not use assertions for argument checking in public methods Use assertions to reaffirm assumptions –Good example of this is in a switch statement with no default case

CSCI 1302 – Exceptions and Assertions28 Exception Handling or Assertions Good use for an assertion: switch (month) { case 1: … ; break; case 2: … ; break; … case 12: …; break; default: assert false: “Invalid month: “ + month; }