Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 15 – Exception Handling Outline 15.1 Introduction 15.2 Exception-Handling Overview 15.3 Exception-Handling.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
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.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
Exception Handling. Lecture Objectives To learn how to throw exceptions To be able to design your own exception classes To understand the difference between.
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 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
Exception Handling (Chapter 8) CS 180 Recitation - February 29, 2008 Department of Computer Science Purdue University.
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.
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.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
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.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
CSE 1201 Object Oriented Programming
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Testing and Exceptions
Advanced Programming Behnam Hatami Fall 2017.
EE422C Software Implementation II
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling and Reading / Writing Files
Fundamental Error Handling
Lecture 11 Objectives Learn what an exception is.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Chapter 12 Exception Handling and Text IO Part 1
Java Basics Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 15 – Exception Handling
Exceptions Review Checked Vs. Unchecked Exceptions
Presentation transcript:

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10

Note Set 10 Overview Exception Handling

exception Exception – an indication of a problem that occurs during a program’s execution Exception handling allows the graceful handling of errors in a program. Goal – allow the program to continue executing as if no problem had been encountered Can somewhat separate error handling code from regular processing code

The standard Do Something (processing) Check to see if it worked (error checking) Do something else Check to see if it worked Do Something Check to see if it worked

Attempt to divide; denominator may be zero Read input; exception occurs if input is not a valid integer

Stack Trace

Exception Handling Code try { //put code here that might // throw an exception //if exception is thrown, move to catch } catch (InputMismatchException e) { //Handle the exception case here – do what is //needed to gracefully recover from the //exception } catch (Exception e) { //Can handle multiple types of exceptions } Exception handlers Uncaught exception – exception for which there is no exception handler - exception type doesn’t match any of the catch blocks Try Block Exception Parameter

ExceptionTest public class ExceptionTest { public static void main (String [] args) { int x = 3; int y = 0; int z = x / y; System.out.println(z); } throws ArithmeticException run: Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTest.main(ExceptionTest.java:9) Java Result: 1

ExceptionTest2 public class ExceptionTest2 { public static void main (String [] args) { int x = 3; int y = 0; int z = 0; try { z = x / y; } catch (ArithmeticException e) { System.out.println("Exception Caught"); } System.out.println(z); } run-single: Exception Caught 0

With Scanner import java.util.*; public class ExceptionTest3 { public static void main (String [] args) { Scanner s = new Scanner(System.in); int x = 0; int y = 0; try { x = s.nextInt(); } catch (InputMismatchException e) { e.printStackTrace(); }

Use Java API JAVA API lists the exceptions thrown by each method

Termination Model of Exception Handling try { code line 1 code line 2 code line 3 } catch (Exception e) { handler 1 handler 2 } code line 4 code line 5 if line 2 throws exception, control transfers to exception handler then to line of code after handlers.

Objectville All exception classes directly or indirectly extend java.lang.Exception. It is possible to create your own exception classes by extending java.lang.Exception.

Objectville Unchecked Exceptions Subclass of RuntimeException Not required to be handled Checked Exceptions Subclass of Exception Must be handled or explicitly or declared w/ throws

throws public int myFunction () throws MySpecialException { //Code here that could throw a MySpecialException //but you don’t want to handle it here for some reason //so you have to declare that this method could also throw //MySpecialException – a checked exception }