EE422C Software Implementation II

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

Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
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.
COMP 121 Week 5: Exceptions and Exception Handling.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to –Improve the reliability.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
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.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
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.
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.
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.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CSE 1201 Object Oriented Programming
Eighth Lecture Exception Handling in Java
16 Exception Handling.
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Why exception handling in C++?
CS1101: Programming Methodology Recitation 7 – Exceptions
CS102 – Exceptions David Davenport Latest: May 2015
COMPSCI 230 S Programming Techniques
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Chapter 15 – Exception Handling
Advanced Programming Behnam Hatami Fall 2017.
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exceptions & exception handling
Exceptions with Functions
Chapter 12 Exception Handling and Text IO
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 Chapter 9 Edited by JJ.
Exception Handling.
Exception Handling in Java
Web Design & Development Lecture 7
Exception Handling in Java
Managing Errors and Exceptions
Lecture 11 Objectives Learn what an exception is.
CMSC 202 Exceptions 2nd Lecture.
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
Exceptions 10-May-19.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

EE422C Software Implementation II Exceptions

Exception Handling An exception is a run-time error - you can handle it or let the JVM handle it Exceptions can cause a program to crash OR they can be caught and handled by your program, hopefully recovering gracefully. Exception handling - using the features of the language to manage run time errors in an orderly fashion

Goals To learn how to catch exceptions To know when and where to catch an exception To learn how to throw exceptions To be able to design your own exception classes To understand the difference between checked and unchecked exceptions

Why Use Exceptions? Separate normal from exceptional behavior For example, what to do if a function’s precondition is violated We want to separate Error Handling Code from Regular Code So that frequent and repetitive testing of return values, and propagation of error return values, is not required. Using exceptions may be more efficient because the normal execution path does not need to test for error conditions.

Why Use Exceptions? Necessary for reliable, fault tolerant, robust software systems Often 75%+ code dedicated to reliability 20% of interface errors are errors in handling exceptions We want to manage run time errors in an orderly fashion so as to create fault tolerant software systems If an exception is not caught, the program terminates. Advanced error handling can be done that allows for resumption of execution after correcting for an error Capability to return error information is not limited to just the return type of an operation.

Exception Handling Options What to do about exceptions Preclude Guarantee they do not happen Report Up to someone else to do something Retry Works when fault is transient Repair in situ Fix the problem or compensate for the problem Ignore Results are satisfactory even with the exception

Why Exceptions - Details We want to propagate Errors Up the Call Stack The point at which an error occurs is rarely a suitable place to handle it, particularly in library code, but by the time an error code has been propagated to a place where it can be handled, too much contextual information has been lost. Exceptions bridge this gap - they allow specific error information to be carried from the point where its available to a point where it can be utilized (using exception objects that contain the information).

Exception Handling Mechanics An exception is an abnormal condition that arises in a code sequence at run time (i.e., a run time error) JAVA has built-in exception handling features for certain exception object types Exception object - is created when the error occurs and contains information about it Exception handling is accomplished by the keywords: try, catch, throw, throws, and finally Exceptions and Exception Types Claiming Exceptions Throwing Exceptions Catching Exceptions Rethrowing Exceptions The finally Clause

Exception Handling Block try { // block of statements to be monitored } catch (ExceptionClass1 exceptionObject) { // block of statements that handle exception type 1 catch (ExceptionClass2 exceptionObject) { // block of statements that handle exception type 2 finally { // block of final statements to be done // in any and all cases

Exception Control Flow When an exception is thrown, the method containing the throw statement doesn't necessarily return immediately to its caller Instead, the closest matching catch clause is located and executed, and then the corresponding finally clause is executed If there is no matching catch clause, the program terminates

Catching Exceptions - Example static void exceptionExample() { int a = 0, b = 0, c = 0, n = 0; Scanner in = new Scanner(System.in); try { // block of code to be monitored for exceptions String s; a = 356 / n; s = in.next(); b = Integer.parseInt (s); } catch (ArithmeticException e1) { //catch arithmetic run time errors System.out.println ("divide by zero attempted"); a = 0; catch (NumberFormatException e2) { //catch invalid conversions of a string to a numeric format System.out.println ("invalid number in string"); b = 0; finally { //do this block regardless c = a + b; System.out.println ("the value of c is " + c);

Throwing Exceptions - Hints It is not necessary to catch all exceptions If you don't know how to handle an exception, don't catch it You can throw them yourself When an exception is thrown, method terminates immediately Execution continues with an exception handler public void deposit(double amount) { if (amount < 0) throw new IllegalArgumentException("cannot deposit negative amount"); balance = balance + amount; }

Example bad use of exceptions [Bloch, Effective Java, Item 39] Do not use exceptions for normal control flow E.g., do not use try { int i = 0; while (true) a[i++].f(); } catch(ArrayIndexOutOfBoundsException e) { } Instead of for(int i = 0; i < a.length; i++) { a[i].f(); }