Sadegh Aliakbary Sharif University of Technology Fall 2010.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions 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.
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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.
Index Exception handling Exception In Java Exception Types
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
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:
For use of Cleveland State's IST410 Students only 1 Exception.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
CS203 Java Object Oriented Programming Errors and Exception Handling.
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.
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.
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.
Object Oriented Programming with Java (150704).  Throwable Exception (This class will catch exceptions generated by prog.) (Create your own custom exception.
Peyman Dodangeh Sharif University of Technology Spring 2015.
Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Peyman Dodangeh Sharif University of Technology Fall 2013.
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.
Introduction to Exception Handling and Defensive Programming.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
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.
1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error= ; double x0.
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.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
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.
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.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
Advanced Programming in Java
Chapter 10 – Exception Handling
Advanced Programming in Java
Advanced Programming Behnam Hatami Fall 2017.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
ATS Application Programming: Java Programming
OBJECT ORIENTED PROGRAMMING
Web Design & Development Lecture 7
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.
Exception Handling.
Presentation transcript:

Sadegh Aliakbary Sharif University of Technology Fall 2010

Agenda Exception Handling Fall 2010Sharif University of Technology2

Review Fall 2010Sharif University of Technology3

Benefits of Exception Handling Framework Separating Error-Handling code from “regular” business logic code Propagating errors up the call stack Grouping and differentiating error types Fall 2010Sharif University of Technology4

Separating Error-Handling Code Consider pseudocode method It reads an entire file into memory readFile { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } Fall 2010Sharif University of Technology5

Traditional Programming Fall 2010Sharif University of Technology6

With Exception Handling Framework Fall 2010Sharif University of Technology7

Note You should still write code for Detecting, Reporting and Handling exceptions Exception handling framework is not responsible for these jobs! It only helps you organize the work more effectively Fall 2010Sharif University of Technology8

Propagating Errors Up the Call Stack Traditional approach Each method should explicitly forward the exception Use a special return code Using return type for reporting exceptions Smells bad! New approach Automatic Beautiful! Fall 2010Sharif University of Technology9

Grouping and Differentiating Error Types All exceptions thrown within a program are objects The grouping or categorizing of exceptions is a natural outcome of the class hierarchy Fall 2010Sharif University of Technology10

Fall 2010Sharif University of Technology11

Example2 class MultipleCatch { public static void main(String args[]) { try { int den = Integer.parseInt(args[0]); System.out.println(3/den); } catch (ArithmeticException exc) { System.out.println(“Divisor was 0.”); } catch (ArrayIndexOutOfBoundsException exc2) { System.out.println(“Missing argument.”); } System.out.println(“After exception.”); } Fall 2010Sharif University of Technology12

Nested Tries class NestedTryDemo { public static void main(String args[]){ try { int a = Integer.parseInt(args[0]); try { int b = Integer.parseInt(args[1]); System.out.println(a/b); } catch (ArithmeticException e) { System.out.println(“Div by zero error!"); } } catch (ArrayIndexOutOfBoundsException) { System.out.println(“Need 2 parameters!"); } Fall 2010Sharif University of Technology13

Bad Use of Exceptions Don’t Use Exception instead of If-else Use exceptions for exceptions! Fall 2010Sharif University of Technology14

Writing Your Own Exceptions your classes should extend Exception class Only Exception Subclasses could be thrown and caught Steps to follow Create a class that extends the RuntimeException or the Exception class Customize the class Members and constructors may be added to the class Fall 2010Sharif University of Technology15

Example class HateStringExp extends Exception { /* some code */ } String input = "invalid input"; try { if (input.equals("invalid input")) { throw new HateStringExp(); } System.out.println("Accept string."); } catch (HateStringExp e) { System.out.println("Hate string!”); } Fall 2010Sharif University of Technology16

Finally try { //.. } catch (ExceptionType e) { //… }... } finally { } Contains the code for cleaning up after a try or a catch Fall 2010Sharif University of Technology17

Finally (2) Block of code is always executed Despite of different scenarios: Normal completion Forced exit occurs using a return, a continue or a break statement Caught exception thrown Exception was thrown and caught in the method Uncaught exception thrown Exception thrown was not specified in any catch block in the method Fall 2010Sharif University of Technology18

Fall 2010Sharif University of Technology19

Unchecked Exceptions private static void function(String[] args) { int den = Integer.parseInt(args[0]); System.out.println(3 / den); } public static void main(String[] args) { function(args); } function() may throw exceptions But it has not declared it with throws keyword Why? Fall 2010Sharif University of Technology20

Checked and Unchecked Exceptions Checked exception Java compiler checks the program should catch or list the occurring exception If not, compiler error will occur Unchecked exceptions Not subject to compile-time checking for exception handling Built-in unchecked exception classes Error RuntimeException Their subclasses Fall 2010Sharif University of Technology21

Exception Class Hierarchy Fall 2010Sharif University of Technology22

Exception Classes and Hierarchy Multiple catches should be ordered from subclass to superclass class MultipleCatchError { public static void main(String args[]){ try { int a = Integer.parseInt(args [0]); int b = Integer.parseInt(args [1]); System.out.println(a/b); } catch (ArrayIndexOutOfBoundsException e) { //.. } catch (Exception ex) { //.. } Fall 2010Sharif University of Technology23

Further Reading Assertions assert name!=null; Fall 2010Sharif University of Technology24

Fall 2010Sharif University of Technology25

References Fall 2010Sharif University of Technology26