Topics Introduction Types of Errors Exceptions Exception Handling

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.
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Yoshi
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
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.
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
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:
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
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.
Finding and Debugging Errors
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.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Java Programming: Guided Learning with Early Objects
VB.Net - Exceptions Copyright © Martin Schray
COMP Exception Handling Yi Hong June 10, 2015.
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.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
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.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Error Handling Tonga Institute of Higher Education.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
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.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
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:
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.
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.
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Introduction to Exceptions in Java
EXCEPTION HANDLING OR ERROR HANDLING.
Managing Errors and Exceptions
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.
Presentation transcript:

Topics Introduction Types of Errors Exceptions Exception Handling Common JAVA Exceptions Syntax of Exception Handling Code Example: Divide by Zero without Exception Handling Example: Handling Arithmetic Exception Example: Multiple Catch Statement Java Exception Hierarchy Finally Block Example with finally block Using throw Keyword Example

Introduction An error may produce an incorrect output or may terminate the execution of the program abruptly or even may cause the system to crash. it is therefore important to detect and manage properly all the possible error conditions in the program so that the program will not terminate or crash during execution.

Types of Errors Errors may broadly be classified into two categories. They are Compile – Time errors All syntax errors will be detected and displayed by the JAVA compiler and therefore these errors are known as compile – time errors. Most of the compile – time errors are due to typing mistakes. The most common error are. Missing semicolons Missing brackets in classes and methods Misspellings of identifiers and keywords Missing double quotes in strings Use of undeclared variables Incompatible types in assignments / initializations use of = in place of == operator And so on.

Types of Errors Errors may broadly be classified into two categories. They are Run – Time errors After compilation process is completed, the program has to be tested for various inputs. If the program produce wrong results due to wrong logic or may terminate due to errors. Such errors are called Run – Time errors. Most common run – time errors are. Diving an integer by 0. Accessing an element that is out of bounds of an array. Attempting to use a negative size for an array. Trying to store a value of an incompatible types. Converting invalid String to a number Accessing a character that is out of bounds of a String. And many more. When such errors are encountered, java typically generates an error message and aborts the program

Exceptions An Exception is a condition that is caused by a run – time error in the program. When JAVA interpreter encounters an error, it creates an Exception object and throws it. if the exception object is not caught and handled properly, the interpreter will display an error message and will terminate the program. if the programmer want the program to continue with execution of the remaining code, then the programmer should try to catch the exception object thrown by the error condition and then display an appropriate message for taking corrective actions. This task is known as Exception Handling.

Exception Handling The purpose of Exception Handling mechanism is to provide a means to detect and report an “Exception Circumstance” so that appropriate action can be taken. Exception Handling performs the following tasks Find the problem (HIT the exception). Inform that an error has occurred (THROW the exception). Receive the error information (CATCH the exception). Take corrective actions (HANDLE the exception). The exception handling code basically consists of two segments. One to detect error and to throw exceptions and the other to catch exception and to take appropriate actions.

Common Java Exceptions The programmer always be on the lookout for places in the program where exception could be generated. Some common exceptions that we must watch out for catching are listed. Exception Type Cause of Exception ArithmeticException Caused by math errors such as division by zero ArrayIndexOutOfBoundException Caused by bad array indexes ArrayStoreException Caused when a program tries to store the wrong type of data in an array FileNotFoundException Caused by an attempt to access a nonexistent file NullPointerException Caused by referencing a null object NumberFormatException Caused when a conversion between strings and numbers fails StackOverFlowException Caused when the system runs out of stack space.

Syntax of Exception Handling The basic concepts of exception handling are throwing an exception and catching it. This is illustrated in figure as shown. Exception object creator try block Statement that causes an exception Throws exception object Exception handler catch block Statement that handles the exception

Syntax of Exception Handling Java uses the keyword try to preface a block of code that is likely to cause an error condition throw an exception. A catch block defined by the keyword catch, catches the exception thrown by the try block and handles it appropriately. The catch block is added immediately after the try block. the try block can have one or more statements that could generate an exception, the remaining statements in the block are skipped and execution jumps to the catch block. The catch block too can have one or more statements that are necessary to process the exception. try block{ statement; }catch(Exceptiontype ex){ }

Example: Divide by Zero without Exception

Example: Divide by Zero without Exception Output:

Example: Handling Arithmetic Exception

Example: Handling Arithmetic Exception Output:

Example: Multiple Catch Statements

Example: Multiple Catch Statements Output:

Example: Multiple Catch Statements Output:

Example: Multiple Catch Statements Output:

Java Exception Hierarchy Exception classes inherit directly or indirectly from class Exception, forming an inheritance hierarchy. Can extend this hierarchy with your own exception classes. Figure  shows a small portion of the inheritance hierarchy for class Throwable (a subclass of Object), which is the superclass of class Exception. Only Throwable objects can be used with the exception-handling mechanism. Class Throwable has two subclasses: Exception and Error.

Java Exception Hierarchy

Finally Block The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling. it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. The finally block is used for resource de-allocation. Placed after the last catch block.

Example: With Finally Block Output:

Throwing Our own Exceptions The uses can do by using the keyword throw as follows throw new Throwable_subClass; Example: throw new ArithmeticException(); throw new NumberFormatException();

Example using Throw

Example using Throw Output:

?