Exception Handling Basic -- from generic idea to Java.

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

Yoshi
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.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
COMP 121 Week 5: Exceptions and Exception Handling.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
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.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Handling Errors with Exception (in Java) Project 10 CSC 420.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
May 21, ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Programming Languages (ICE 1341) Lecture #21 Programming Languages (ICE 1341)
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CIS 270—Application Development II Chapter 13—Exception Handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
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.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
How to Design Error Steady Code Ivaylo Bratoev Telerik Corporation
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
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.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
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.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exception Handling SWE 344 Internet Protocols & Client Server Programming.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
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.
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.
Java Exceptions a quick review….
Tirgul 13 Exceptions 1.
Introduction to Exceptions in Java
Exceptions The Need for Exceptions Throwing Exceptions
Handling Exceptions.
Part B – Structured Exception Handling
Web Design & Development Lecture 7
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Java Exceptions Dan Fleck CS211.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

Exception Handling Basic -- from generic idea to Java

Daily Idea In our life, there are something that we don't expect to happen but they happens; or something that's rare but still happens. We can consider these things exception. Blackout Snow day We react to these things (or called event) in some way to prevent loss, this can be called exception handling.

Daily Idea Example: Snow day Consider a school-student system. When there is a snow storm coming up. Different parties (school & student) should act properly to prevent loss. School: decide if they should close for the day and setup a voice message in its phone to notify the temporary of school. Student: check by phone if school close for the day.

Daily Idea Example: Snow day School Student Phone message Setup Check The reaction of both parties to communicate, such that the whole system react correctly to prevent loss:

Daily Idea Example: Snow day The party that do not communicate correctly or react correctly can gain loss for them-self or the other parties. Schools that does not close for extremely strong storm: may gain a law sue from students or employees Students that come to school without checking: may waste time higher probability of car accident

Exceptions in Software Different parties of the software should communicate together and react together correctly to avoid loss as much as possible. For the term “different parties” we mean: modules or routines/methods/functions that have different responsibilities and work together.

Exceptions in Software In software systems, there are many situation that can be considered exceptions: division by zero number underflow or overflow not enough memory hardware failure try to access a file but file missing...

Exceptions in Software try-catch mechanism try-catch mechanism is used by currently many programming languages to communicate each other and handle exceptions: Java (we will use this to study for now) C++ Object Pascal Matlab

Java Exception Handling Example: division by zero

Java Exception Handling Example: division by zero The program compiles and runs, but we may not be happy with the results: If the div() routine is directly or indirectly used by more other methods to get more results, we may want an explicit notification to calling method instead of seeing more Infinity or NaN.

Java Exception Handling Example: division by zero

Java Exception Handling Example: division by zero Now we will not see Infinity and NaN, but notified explicitly of exception.

Java Exception Handling Example: division by zero Question: Why not just let div() method handle the exception by itself, but throw an Exception object to its caller and let its caller handle it?

Java Exception Handling Example: division by zero Question: Why not just let div() method handle the exception by itself, but throw an Exception object to its caller and let its caller handle it? Answer: Because the implementer of div() may not know what the user of div() want in case of exception. It shall leave it for the caller.

Call Stack view of throwing Sun's lesson of Exception

Call Stack view of throwing Sun's lesson of Exception

Call Stack view of throwing A handler can either: Really handle the exception, or throw another Exception

Some times there will be code that should be executed doesn't matter if exception is caught or not. For example, resources need to be released even if some operations to the resource fails. These code can be put into a finally block. Java Exception Handling finally block

Java Exception Handling finally block try{ // the code that try to be run } catch(...){// first handler }.... catch(...){// last handler } finally{// the code that will be run anyways }

Java Exception Handling finally block example public void writeList() { PrintWriter out = null; try { out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) out.println(vector.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println(e.getMessage()); } catch (IOException e) { System.err.println(e.getMessage()); } finally { if (out != null) out.close(); }

public void writeList() { PrintWriter out = null; try { out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) out.println(vector.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println(e.getMessage()); } catch (Exception e) { System.err.println(e.getMessage()); } finally { if (out != null) out.close(); } Java Exception Handling finally block example Note this section of code may not compile because it is just for demo purpose and lack of pre- requested class definitions. The code is the result of simplifying code from

public void writeList() { PrintWriter out = null; try { out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) out.println(vector.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println(e.getMessage()); } catch (Exception e) { System.err.println(e.getMessage()); } finally { if (out != null) out.close(); } Java Exception Handling finally block example Note this section of code may not compile because it is just for demo purpose and lack of pre- requested class definitions. The code is the result of simplifying code from

Exceptions are classes in Java from Basic%20Exception%20Handling.pdf

References Sun's lesson of Exception Java Intro Programming (With Passion!) Online Boot Camp