Exception Handling in Java

Slides:



Advertisements
Similar presentations
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Advertisements

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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
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.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
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.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
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.
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.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
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.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
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.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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.
CSE 1201 Object Oriented Programming
Exceptions: When things go wrong
Chapter 10 – Exception Handling
Advanced Programming in Java
Tirgul 13 Exceptions 1.
Chapter 13 Exception Handling
Topic: Exception Handling
Introduction Exception handling Exception Handles errors
COMPSCI 230 S Programming Techniques
Multithreading in Java
Exception Handling Visit for more Learning Resources.
Advanced Programming Behnam Hatami Fall 2017.
EE422C Software Implementation II
CNS 3260 C# .NET Software Development
ATS Application Programming: Java Programming
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
TRY CATCH BLOCK By Kosala Rajapaksha.
Web Design & Development Lecture 7
Managing Errors and Exceptions
Chapter 13 Exception Handling
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.
Tutorial Exceptions Handling.
Chapter 12 Exception Handling and Text IO Part 1
Tutorial MutliThreading.
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.
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Exception Handling in Java

Rest of the code is executed An object of exception class is thrown exception object Int data=10/0; Is handled? No Yes JVM 1)Prints out exception description 2) Prints the stack trace 3) Terminate the program Rest of the code is executed

Try-catch Block try{ } catch (exception(type) e(object))‏ { //statements that may cause an exception } catch (exception(type) e(object))‏ { //error handling code

Multiple catch try { //Protected code } catch(ExceptionType1 e1) { //Catch block catch(ExceptionType2 e2) ……

Sequence of Events Preceding step try block statement unmatched catch matching catch unmatched catch next step

try{ int a[]=new int[7]; a[4]=30/0; } catch(ArithmeticException e) { class Example2{ public static void main(String args[]){ try{ int a[]=new int[7]; a[4]=30/0; } catch(ArithmeticException e) { System.out.println("Warning: ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Warning:ArrayIndexOutOfBoundsException"); } catch(Exception e){ System.out.println("Warning: Some Other exception"); } System.out.println("Out of try-catch block..."); }

Nested try catch One try-catch block can be present in the another try’s body. This is called Nesting of try catch blocks. Each time a try block does not have a catch handler for a particular exception, the stack is unwound and the next try block’s catch (i.e., parent try block’s catch) handlers are inspected for a match. If no catch block matches, then the java run-time system will handle the exception.

try { statement 2; } catch(Exception e1) { try { statement 1; Syntax of Nested try Catch try { statement 1; try { statement 2; } catch(Exception e1) { //Exception Message } } catch(Exception e2) //Catch of Main(parent) try block { //Exception Message

What is Finally Block A finally statement must be associated with a try statement. It identifies a block of statements that needs to be executed regardless of whether or not an exception occurs within the try block. It will run regardless of whether an exception was thrown and handled by the try and catch parts of the block.

Try-catch-finally In normal execution the finally block is executed after try block. When any exception occurs first the catch block is executed and then finally block is executed. Try { …… } catch(…) { …….. Finally Try { ……. } Finally ………

Finally block is executed Program Code Exception Occurred ? no Yes Exception Handled ? no Yes Finally block is executed

Sequence for finally clause Preceding step try block statement unmatched catch matching catch unmatched catch finally next step

public static void main(String args[]){ try{ class Simple{     public static void main(String args[]){     try{      int data=25/0;      System.out.println(data);     }     catch(ArithmeticException e){ System.out.println(e); }     finally{ System.out.println("finally block is always executed");   System.out.println("rest of the code...");     }  }  

Throwing our Own Exceptions throw keyword In java we have already defined exception classes such as ArithmeticException, NullPointerException etc. These exceptions are implicitly thrown by JVM The throw keyword is used to explicitly throw an exception. These exceptions are known as user-defined exceptions.

Syntax of throw statement throw new AnyThrowableInstance; IOException e = new IOException(); throw e;

class MyException extends Exception { public MyException (String msg) { super(msg); } } class TestMyException { public static void main(String[] args) { int age=-2; try { if(age < 0) throw new MyException("Age can't be less than zero"); catch (MyException e) { e.printStackTrace();

Syntax of throws keyword: The throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception. So it is better for the programmer to provide the exception handling code so that normal flow can be maintained. Syntax of throws keyword: void method_name() throws exception_class_name {     ...    }  

void method() throws IOException{ import java.io.*;   class M {    void method() throws IOException{     throw new IOException("device error");    }   }   class Test{      public static void main(String args[])throws IOException{     Test t=new Test();       t.method();          System.out.println("normal flow...");     }  

throw keyword throws keyword throw is used to explicitly throw an exception. throws is used to declare an exception. checked exception can not be propagated without throws. checked exception can be propagated with throws. throw is followed by an instance. throws is followed by class. throw is used within the method. throws is used with the method signature. You cannot throw multiple exception You can declare multiple exception e.g. public void method()throws IOException,SQLException.

Common Questions What is exception? How it is handled? Explain with suitable example. Explain following terms with respect to exception try catch throw Finally What are different types of errors? What is use of throw, throws, finally. Write a program to throw a user defined exception “String Mismatch” if two strings are not equal.