[go: up one dir, main page]

0% found this document useful (0 votes)
197 views6 pages

Exception Handling

The document discusses exception handling in Java. It defines exceptions as abnormal conditions that disrupt normal program flow. It describes the exception hierarchy in Java with Throwable at the top, and Exception and Error as subclasses. It also discusses checked and unchecked exceptions, and the keywords try, catch, finally, throw, and throws used in exception handling.

Uploaded by

honaday945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views6 pages

Exception Handling

The document discusses exception handling in Java. It defines exceptions as abnormal conditions that disrupt normal program flow. It describes the exception hierarchy in Java with Throwable at the top, and Exception and Error as subclasses. It also discusses checked and unchecked exceptions, and the keywords try, catch, finally, throw, and throws used in exception handling.

Uploaded by

honaday945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Exception Handling: Introduction

 The Exception Handling in Java is one of the powerful mechanism to handle the runtime
errors so that normal flow of the application can be maintained.

 What is Exception in Java

 Dictionary Meaning: Exception is an abnormal condition.

 In Java, an exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime

 Exception Handling is a mechanism to handle runtime errors such as


ClassNotFoundException, IOException, SQLException, RemoteException, etc.

 Hierarchy of Java Exception classes

 The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited
by two subclasses: Exception and Error. A hierarchy of Java Exception classes are given
below:

Keyword Description
try The "try" keyword is used to specify a block where

we should place exception code.

The try block must be followed by either catch or finally. I

t means, we can't use try block alone.

catch The "catch" block is used to handle the exception.

It must be preceded by try block which means we can't use catch block alone.

It can be followed by finally block later.

finally The "finally" block is used to execute the important code of the program.

It is executed whether an exception is handled or not.

throw The "throw" keyword is used to throw an exception.

throws The "throws" keyword is used to declare exceptions.

It doesn't throw an exception.

It specifies that there may occur an exception in the method.

It is always used with method signature.

 There are mainly two types of exceptions: checked and unchecked. Here, an error is
considered as the unchecked exception. According to Oracle, there are three types of
exceptions:

 Checked Exception

 Unchecked Exception

 Error

3) Error

 Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

class Ae

public static void main(String args[])

int a=10,b=0;

try

System.out.println("division="+(a/b));
}

catch(ArithmeticException e)

System.out.println("divide by zero exception");

finally{

System.out.println("it is always ececuted");

Output:

divide by zero exception

it is always ececuted

class Ex2

public static void main(String args[])

int a=0,b=10;

String s="java";

try{

System.out.println(b/a);

int i=Integer.parseInt(s);

System.out.println(i);

s=null;

System.out.println(s.length());

catch(ArithmeticException e)

System.out.println("/ by zero exception");

catch(NullPointerException e)
{

System.out.println(" Null pointer exception");

catch(NumberFormatException e)

System.out.println("Number Format exception");

Output:

/ by zero exception

Checked vs Unchecked Exceptions in Java

1) Checked: are the exceptions that are checked at compile time. If some code within a
method throws a checked exception, then the method must either handle the exception or
it must specify the exception using throws keyword.

For example, consider the following Java program that opens file at location “C:\test\a.txt”
and prints the first three lines of it. The program doesn’t compile, because the function
main() uses FileReader() and FileReader() throws a checked
exception FileNotFoundException. It also uses readLine() and close() methods, and these
methods also throw checked exception IOException

2) Unchecked are the exceptions that are not checked at compiled time. In C++, all
exceptions are unchecked, so it is not forced by the compiler to either handle or specify the
exception. It is up to the programmers to be civilized, and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions,
everything else under throwable is checked.

Java Multi-catch block

 A try block can be followed by one or more catch blocks. Each catch block must contain a
different exception handler. So, if you have to perform different tasks at the occurrence of
different exceptions, use java multi-catch block.

 At a time only one exception occurs and at a time only one catch block is executed.

 All catch blocks must be ordered from most specific to most general, i.e. catch for
ArithmeticException must come before catch for Exception

Java finally block

 Java finally block is a block that is used to execute important code such as closing
connection, stream etc.

 Java finally block is always executed whether exception is handled or not.


Java finally block follows try or catch block. The throws
keyword
Whenever an exception occurs in a method you need to handle it by wrapping the
code that caused exception within the try-catch block or, you can throw/postpone it
using to the calling method using the throws keyword. Then you need to handle the
exception at the calling method.

he throw keyword
You can throw a user-defined exception or, a predefined exception explicitly using
the throw keyword.
There are two types of exceptions user-defined and predefined each exception is
represented by a class and which inherits the Throwable class.
To throw an exception explicitly you need to instantiate the class of it and throw its
object using the throw keyword.
Example
1. public class TestThrow {
2. //defining a method
3. public static void checkNum(int num) {
4. if (num < 1) {
5. throw new ArithmeticException("\nNumber is negative, cannot calculate square");
6. }
7. else {
8. System.out.println("Square of " + num + " is " + (num*num));
9. }
10. }
11. //main method
12. public static void main(String[] args) {
13. TestThrow obj = new TestThrow();
14. obj.checkNum(-3);
15. System.out.println("Rest of the code..");
16. }
17. }
Output:
Number is negative, cannot calculate square
public class TestThrows {
2. //defining a method
3. public static int divideNum(int m, int n) throws ArithmeticException {
4. int div = m / n;
5. return div;
6. }
7. //main method
8. public static void main(String[] args) {
9. TestThrows obj = new TestThrows();
10. try {
11. System.out.println(obj.divideNum(45, 0));
12. }
13. catch (ArithmeticException e){
14. System.out.println("\nNumber cannot be divided by 0");
15. }
16.
17. System.out.println("Rest of the code..");
18. }
19. }

Output:

Number cannot be divided by 0

Rest of the code

You might also like