Exception Handling Part 1: Handing Java Exceptions CSIS 3701: Advanced Object Oriented Programming.

Slides:



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

1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Exception Handling Xiaoliang Wang, Darren Freeman, George Blank.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
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.
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.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions CIS 304 Intermediate Java Programming for Business.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
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.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 CSC241: Object Oriented Programming Lecture No 27.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
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 Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Chapter 8-Exception Handling/ Robust Programming.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
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.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Introduction to Exceptions in Java
Introduction to Exceptions in Java
COMPSCI 230 S Programming Techniques
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Web Design & Development Lecture 7
Exception Handling in Java
Managing Errors and Exceptions
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Java Basics Exception Handling.
Exceptions Review Checked Vs. Unchecked Exceptions
Presentation transcript:

Exception Handling Part 1: Handing Java Exceptions CSIS 3701: Advanced Object Oriented Programming

What is an Exception? Error: Fault in program that causes it to incorrectly implement abstract concept –Example: pop method that does not remove value from stack Exception: Situation where abstract concept undefined –Example: popping from empty stack Often caused by user error

Examples of Exceptions Abstract conceptException Division0 / 0 Square root sqrt(x) Negative numbers Array access A[index]index outside bounds of array Reference access object.method() object is null Problem: Must still somehow handle these problems if they occur in program!

Handling Exceptions What are options for handling exceptions? –Ignore problem (array out of bounds in C) –Shut down program (null pointer in C) –Print message… What is best option? –Bank software  Save data, shut down, report error –Autopilot software  Display alert, but keep running! No universal best way to handle exception Only user of class can decide how to handle!

Detecting/Handling Exceptions Call that causes exception s.pop(); Code to handle exception User code Code to detect exception public int pop(){ if (top == 0) Must signal exception to user Class code call signal

Signaling Exceptions How can your code signal user about exception? Return a value (such as true/false)? –User can ignore signal (like any other return value) –Method may already be returning a value pop() cannot return true/false, as must return an int Must signal exception without returning a value –Code throws an exception object to caller –Caller catches exception object and handles problem in whatever way considered appropriate

Throwing/Catching Exceptions try { call that might cause exception … } catch(ExceptionType e) { code to handle exception } User code method(…) { code to detect exception throw new ExceptionType(); } Class code Exception object thrown back to caller

Exception Handling Syntax … try { Call that may cause exception Any other code that should not be called if exception occurs } catch(ExceptionType e) { code to handle this type of exception } code after exception handing If exception is thrown, skip to corresponding catch and execute its code Then skip to first code after try/ catch block If no exception is thrown by end of try block, skip catch blocks

Multiple Exceptions … try { Call that may cause exception Any other code that should not be called if exception occurs } catch(ExceptionType1 e) { code to handle this type of exception } catch(ExceptionType2 e) { code to handle this type of exception } code after exception handing May throw either ExceptionType1 or ExceptionType2 Jump to appropriate catch block for type of exception that occurs Then skip to the first statement after all catch blocks

Java Exception Classes Exceptions are classes in Java ExceptionClass Division: 0 / 0 ArithmeticException A[index] outside bounds of array ArrayIndexOutOfBounds Exception Parsing non-numeric string Integer.parseInt(“Fred”) NumberFormatException object.method() for null object NullPointerException Access to nonexistent file IOException

Division Example Example: Prompt for 2 numbers, divide them and display result Key question: What types of exceptions may occur? String s1 = JOptionPane.showInputDialog(“Num: "); String s2 = JOptionPane.showInputDialog("Denom: "); int num = Integer.parseInt(s1); int den = Integer.parseInt(s2); int result = num/den; JOptionPane.showMessageDialog(null, "The result is "+result); May cause NumberFormatException May cause ArithmeticException

Division Example Other questions: –What other code should not be executed if exceptions occur? Should not divide numbers if numbers not entered Should not display result if division cannot be done Therefore, parsing, division, and display must be in same try block –How should exceptions be handled Print appropriate error messages Skip to final message

Division Example try { int num = Integer.parseInt(s1); int den = Integer.parseInt(s2); int result = num/den; JOptionPane.showMessageDialog(null, “Result: "+result); } catch(NumberFormatException numex) { JOptionPane.showMessageDialog(null, “Must be integers!"); } catch(ArithmeticException arex) { JOptionPane.showMessageDialog(null, "Undefined number!"); }

Division Example try { int num = Integer.parseInt(s1); int den = Integer.parseInt(s2); int result = num/den; JOptionPane.showMessageDialog(null, “Result: "+result); } catch(NumberFormatException numex) { JOptionPane.showMessageDialog(null, “Must be integers!"); } catch(ArithmeticException arex) { JOptionPane.showMessageDialog(null, "Undefined number!"); } User enters “fred” for second number

Division Example try { int num = Integer.parseInt(s1); int den = Integer.parseInt(s2); int result = num/den; JOptionPane.showMessageDialog(null, “Result: "+result); } catch(NumberFormatException numex) { JOptionPane.showMessageDialog(null, “Must be integers!"); } catch(ArithmeticException arex) { JOptionPane.showMessageDialog(null, "Undefined number!"); } User enters 0 for first and second number

Division Example try { int num = Integer.parseInt(s1); int den = Integer.parseInt(s2); int result = num/den; JOptionPane.showMessageDialog(null, “Result: "+result); } catch(NumberFormatException numex) { JOptionPane.showMessageDialog(null, “Must be integers!"); } catch(ArithmeticException arex) { JOptionPane.showMessageDialog(null, "Undefined number!"); } User enters 11 for first number and 5 for second

Unwinding the Stack What if exception not handled? –Exit current method –Throw exception to caller –Can handle exception at any level

Unwinding the Stack try { … int result = divide(num, den); JOptionPane.showMessageDialog(null, “Result: "+result); } … catch(ArithmeticException arex) { JOptionPane.showMessageDialog(null, "Undefined number!"); } … } public int divide(int a, int b) { return a/b; } 1) ArithmeticException thrown here, not handled here 2) so ArithmeticException thrown back to here call 3) And handled here

Unwinding the Stack What if exception not handled at any level? Stack completely “unwound” –Methods in side application –Button methods which called application… –Up to main Ultimately thrown to containing JVM –Prints message for each method unwound –Does not cause program crash!