Download presentation
Presentation is loading. Please wait.
1
Java Basics Exception Handling
2
Topics To Be Covered Objective Exception Handling Summary
3
Objectives Exception Handling At the end of this course,
you will be able to understand: Exception Handling
4
Exception Handling
5
Exception Handling A built in mechanism for trapping & handling runtime errors Usually deals with abnormal events or code execution which prevents the program from continuing, like: Array out of bounds accesses Divide by Zero Null pointers & so on...
6
Exception Handling An exception is an event, which occur during the execution of a program that disrupt the normal flow of a program’s instruction. Exception handling in java When error occur the program through an exception, when it through the system attempts to handle it. There are verity of methods to handle this. The list of methods is known as call stack.
7
Exception Handling What is Exception? An Exception is a runtime error
To represent all types of exceptions there Is an Exception class in Java A variety of subclasses allows handling different kinds of errors & abnormal events Basic concept: Whenever an abnormal event occurs. Java throws an Exception It means Java creates an object of subclass of the Exception class Whenever an Exception could possibly be thrown, we must provide a mechanism for catching it in our code
8
Kind of Exception There are three kind of exceptions:
Checked exceptions Errors Runtime exceptions
9
Kind of Exception Checked Exception:
These are exceptional conditions that a well written program should anticipate and recover from. For example an application prompts a user from an input file name, and then open the file by passing the name to the constructor. IOException, SQLException are checked exceptions.
10
Kind of Exception Errors:
These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example an application is successfully opens a file for input, but is unable to read the file because of the hardware of system malefaction . Examples: OutOfMemoryError, VirtualMachineError, AssertionError are errors.
11
Kind of Exception Runtime exceptions:
These are exceptional conditions that are internal to the application, and that are application could not recover or anticipate from. Examples: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException
12
Exception Handling Catching Exception
A try statement executes a block and oversees the execution of enclosed statements for exceptions A try also defines the scope for exception handlers (defined in catch clause) A try block must be accompanied by at least one catch block or one finally block Any method declared as being able to throw an Exception, can have a try / catch block to handle the exception
13
Exception Handling Catching Exception
Java try block is used to enclose the code that might throw an exception. It must be used within the method. Java try block must be followed by either catch or finally block.
14
Exception Handling Catching Exception Syntax of java try-catch try{
//code that may throw exception }catch(Exception_class_Name ref){} Syntax of try-finally block }finally{}
15
Exception Handling Exception Example public class ExceptionDemo1 {
public static void main(String [ ] args) { int [ ] x = {1,2,3}; try { System.out.println(“x[0] = * + x[0]); System.out.println("x[5] = * + x[5]); } catch(ArraylndexOutOfBoundsException ae) { System.out.println("Index Does Not Exist");
16
Exception Handling Exception Example public class Testtrycatch2{
public static void main(String args[]){ try{ int data=50/0; }catch(ArithmeticException e){ System.out.println(e); } System.out.println("rest of the code..."); }
17
Exception Handling Exception Hierarchy Object Throwable Error . . .
RuntimeException Exception Handling Exception Hierarchy
18
Exception Handling Categories of Exception
Java exceptions fall in two categories: Unchecked Not checked by the compiler at compile time Does not force the client program / method to declare each exception thrown by a method, or even handle it All exceptions are derived from RuntimeException class Checked Checked by the compiler to see if these exceptions are properly caught or specified, & if not. the code fails to compile Forces client program to deal with the scenario in which an exception may be thrown All exceptions which are not derived from RuntimeException class
19
Exception Handling Multiple Catch Blocks try { String text = "text";
A method can throw more than one possible Exceptions, or the try block could call two different methods that throw two different Exceptions try { String text = "text"; System.out.println(text.charAt(10)); int n = Integer.parseInt("abc"); } catch(IndexOutOfBoundsException e) { System.err.printIn(“Index out of bounds"); e.printStackTrace(); } catch(NumberFormatException e) { System.err.printIn("bad number"); e.printStackTrace(); }
20
Exception Handling Multiple Catch Blocks try { String text = "text";
Since all Exceptions are subclasses of the Exception class, we can generalize catch blocks to accept multiple different types of Exceptions by using a super class try { String text = "text"; System.out.println(text.charAt(10)); int n = Integer.parseInt("abc"); } catch(Exception e) { System.err.printIn(“Something bad happened"); e.printStackTrace(); }
21
Exception Handling The finally Block
Sometimes, while in a try / catch block, an Exception could be thrown before some important code at the end of the try block The finally block can be used to run this code Code in finally always executes (even in case of unhandled exceptions) try { String text = "text"; System.out.println(text.charAt(10)); } catch(lndexOutOfBoundsException e) { System.err.println("Index out of bounds"); e.printStackTrace(); } finally { System.out.println("This is going to Excecute always"); }
22
Exception Handling Exception Propagation
Exceptions are always propagated from the called method to the caller method, if thrown from the called method If an Exception is thrown from the main() method, it will be propagated to the Java Runtime In exception propagation, all statement executions are ignored until finding the exception handler
23
Exception Handling public class Propagate { void calculate() {
int n = 25, i = 0; i = n/i; } public static void main(String[] args) { Propagate p = new Propagate(); p.calculate(); Exception propagated from calculate () to main () method Arithmetic Exception Occurred Exception in thread “main" java.lang.ArithmeticException: / by zero at Propagate.calculate(Propagate.java:4) at Propagate.main(Propagate.java:8) Exception propagated from main() function to Java
24
Summary
25
Summary In this session, we have covered: Exception Handling
Introduction Checked & Unchecked Exceptions Using try, catch, finally Exception Propagation
26
Sample Exercise #1 What type of exception is divide by zero is ?
This is an athematic exception .
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.