© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer.

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages.
COP INTERMEDIATE JAVA Exception Handling Serialization.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Exception Handling. Background The fact that software modules should be robust enough to work under every situation. The exception handling mechanism.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
Handling Errors with Exception (in Java) Project 10 CSC 420.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions COMPSCI 105 S Principles of Computer Science.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
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.
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.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Slides Credit Umair Javed LUMS Web Application Development.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
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.
Copyright © Mohamed Nuzrath Java Programming :: Syllabus & Chapters :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Lecture 18B Exception Handling and Richard Gesick.
EXCEPTION HANDLING IN C++
Chapter 13 Exception Handling
Exceptions C++ Interlude 3
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
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.
Exception Handling Chapter 9 Edited by JJ.
Chapter 13 Exception Handling
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exception Handling.
Lab 1 Exception Handling.
Presentation transcript:

© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer | Web Developer | Software Engineer IDM Affiliated University College Kandy Campus #725, Peradeniya Rd, Kandy TP TM

© Mohamed Nuzrath Exception Handling The general purpose error processing system in Java is known as exception handling mechanism. An exception, which is short for an exceptional condition, is an abnormal condition that can occur during the execution of a program, If these situations aren’t handled properly, the program will abort or it will continue with incorrect result.

© Mohamed Nuzrath  The java.lang.Throwable class and its sub classes represent all the exceptions that might occur in a program. When an exceptional condition occurs, a exception is thrown, In other words an instance of class java.lang.Throwable or one of its sub classes is created and passed in to the program.

There are two immediate sub classes of java.lang.Throwable, java.lang.Exception and java.lang.Error java.lang.Throwable java.lang.Exceptionjava.lang.Error java.lang.RuntimeException

 The java.lang.Exception class represents abnormal conditions tht can be handled in programs. The java.lang.RuntimeException class (which is a sub class of java.lang.Exception) and its sub classes represent exceptional conditions that are created during runtime. The java.lang.Error class and its sub classes correspond to situations that are not easily predicted (conditions that should not be expected to occur in normal circumstances) such as system running out of memory. You cant write code and fix errors

© Mohamed Nuzrath Handling Exceptions The exception handling mechanism in Java is made up of two parts - throwing exceptions and - catching them

© Mohamed Nuzrath Throwing Exceptions  To throw and exception means to signal an error. Exceptions can be thrown by any Java code or by the runtime system. When an exceptional condition occurs, an exception ( an instance of java.lang.Throwable class or one of its sub class) is created and thrown.

© Mohamed Nuzrath When an exception is thrown, the runtime system stops the execution Of the program at that point. Then it tries to find a place, to transfer the execution, where the exception can be properly handled.

© Mohamed Nuzrath Catching Exceptions After an exception in thrown, the run time system searches for and appropriate exception handler to catch ( to handle ) it. The execution will be transferred to the first handler that the runtime system can find. If the runtime system can’t find any handlers, it will abort the execution of the program.

© Mohamed Nuzrath try and catch keywords The “try” keyword is used to specify a block of code, which is capable of throwing exceptions. Immediately after a try block there should be a catch clause or finally clause. Refer CH6\examples\examples\TryCatchDemo.javaexamples\TryCatchDemo.java