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.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
4/29/20151 Java Exceptions. 4/29/20152 Overview An exception is an unusual circumstance, such as an error condition, that must be handled in a non-standard.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
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.
Lecture 27 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.
Java Computer Industry Lab. 1 Programming Java Exception Handling Incheon Paik.
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.
Object Oriented Programming
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 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.
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.
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.
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.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
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.
CSE 1201 Object Oriented Programming
Advanced Programming in Java
Chapter 14 – Exception Handling
Exceptions In this lecture:
Exceptions: When things go wrong
Chapter 10 – Exception Handling
Advanced Programming in Java
MIT AITI 2003 Lecture14 Exceptions
Java Programming Language
Introduction to Exceptions in Java
Introduction to Exceptions in Java
COMPSCI 230 S Programming Techniques
Testing and Exceptions
CS Week 10 Jim Williams, PhD.
EXCEPTION HANDLING.
Advanced Programming Behnam Hatami Fall 2017.
EE422C Software Implementation II
Exception Handling Chapter 9.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 12 Exception Handling and Text IO
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 OR ERROR HANDLING.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling and Reading / Writing Files
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling Chapter 9 Edited by JJ.
Exception Handling in Java
Web Design & Development Lecture 7
Exception Handling in Java
Managing Errors and Exceptions
Intro to Exceptions (c) Eraj Basnayake
Lecture 11 Objectives Learn what an exception is.
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Tutorial Exceptions Handling.
Chapter 12 Exception Handling and Text IO Part 1
Exceptions.
Object-Oriented Programming (OOP) Lecture No. 44
Why do we need exceptions?
Java Basics Exception Handling.
Exceptions.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

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. An Exception in Java is an Object that’s created when an abnormal situation arises in your program. The Exception Object has data members that store information about the nature of the problem.

Exception Handling class Test { public static void main (String[] args) { int d=0; int a=10/d; } } Uncaught Exception. java.lang.ArithmeticException: / by zero. This Exception is caught by java default handler.

Catching Exception It allow you to fix the errors. It prevents the program from automatically terminating. try and catch Blocks Use try block to identify the code that can throw exceptions. Use catch blocks to catch the exceptions.

try and catch Block Code generates ArithmeticException. class Test { public static void main (String[] args) { int b=0; try { int a=10/b;} scope of a is limited in try block. catch (ArithmeticException e) { System.out.println(“ Divide by zero Exception”);} }

try and catch Blocks Code generates ArithmeticException and ArrayIndexOutOfBoundsException. class Test { public static void main (String[] args) { int a=10; int b[] = new int [5]; try { System.out.println(a/0); System.out.println(b[5]);} catch (ArithmeticException e) { System.out.println(“ Divide by zero Exception”);} catch (ArrayIndexOutOfBoundsException e){ System.out.println(“ Array Index Exception”);} }}

Nested try Statements class Test { public static void main (String[] args) { int a=10; int b[] = new int [5]; try{ try { System.out.println(a/0); System.out.println(b[5]);} catch (ArithmeticException e) { System.out.println(“Divide by zero Exception”);} } catch (ArrayIndexOutOfBoundsException e){ System.out.println(“ Array Index Exception”);} }}

Propagation of Exception class Test { public static void main (String[] args) { Test1 obj1 = new Test1(); try{ obj1.abc();} catch (ArithmeticException e) { System.out.println(“Divide by zero Exception”);} } } class Test1 { abc() { System.out.println(10/0); } }

try, catch and finally class Test { public static void main (String[] args) { Test1 obj = new Test1(); System.out.println(obj.add()); }} class Test1{ int add(){ try { System.out.println(10/10); return 1;} catch (ArithmeticException e) { System.out.println("Divide by zero Exception"); return 2;} finally { System.out.println("finally block"); return 3;}

try and finally class Test { public static void main (String[] args) { Test1 obj = new Test1(); System.out.println(obj.add()); }} class Test1{ int add(){ try { System.out.println(10/10); return 1;} finally { System.out.println("finally block"); return 3;}

Sequence of Catch Blocks Illegal Sequence class Test { public static void main (String[] args) { int a[] = new int [5]; try { System.out.println("abc".substring(3,2)); System.out.println(a[5]); } catch (IndexOutOfBoundsException e) { System.out.println(“ Index Exception");} catch (ArrayIndexOutOfBoundsException e) { System.out.println(“ Array Index Exception");} catch (StringIndexOutOfBoundsException e) { System.out.println(“ String Index Exception");} finally { System.out.println(“ finally block ");} }} General Class cannot comes before Special classes.