Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Exception Handling and

Multithreading
Chapter 4
Marks-12

1 Prof.A.V.Chechare
Introduction
 Error:
An Error indicates serious problem that a
reasonable application should not try to catch or
that application may crash.

 Exception:
An Exception is an unwanted event that interrupts
the normal flow of the program. When an exception
occurs program execution gets terminated. 

2 Prof.A.V.Chechare
ATM Machine (Scenario 1)

When User Don’t


have sufficient
Money

3 Prof.A.V.Chechare
ATM Machine (Scenario 2)

When ATM Don’t


have sufficient
Money

4 Prof.A.V.Chechare
Exception Hierarchy

All exception and errors types are sub classes


of class Throwable, which is base class of
hierarchy.
5 Prof.A.V.Chechare
 An exception can occur for many different
reasons. Following are some scenarios where an
exception occurs.
1.A user has entered an invalid data.
int a=“abc”;
2.A file that needs to be opened cannot be
found.
file name =two.java
trying= tow.java
3.A network connection has been lost in the
middle of communications or the JVM has run
out of memory.
 Some of these exceptions are caused by user
error, others by programmer error, and others by
physical resources that have failed in some
manner.

6 Prof.A.V.Chechare
Types of Exceptions
1)Checked exceptions − A checked exception is an
exception that is checked (notified) by the
compiler at compilation-time, these are also
called as compile time exceptions.
 These exceptions cannot simply be ignored, the
programmer should take care of (handle) these
exceptions.

7 Prof.A.V.Chechare
 For example, if you use FileReader class in your
program to read data from a file, if the file
specified in its constructor doesn't exist, then a
FileNotFoundException occurs, and the compiler
prompts the programmer to handle the exception.
import java.io.File;
import java.io.FileReader;
public class FilenotFound_Demo
{
public static void main(String args[])
{
File file = new File("E://file.txt");
FileReader fr = new FileReader(file);
}
}

8 Prof.A.V.Chechare
 Output:
 C:\>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8: error: unreported
exception FileNotFoundException; must be
caught or declared to be thrown FileReader fr =
new FileReader(file);
1 error

9 Prof.A.V.Chechare
 Unchecked exceptions −
1.An unchecked exception is an exception that
occurs at the time of execution.

2.These are also called as Runtime Exceptions.

3.These include programming bugs, such as


logic errors or improper use of an API.

4.Runtime exceptions are ignored at the time of


compilation.

10 Prof.A.V.Chechare
 For example, if you have declared an array of size 5 in
your program, and trying to call the 6th element of the
array then an
ArrayIndexOutOfBoundsExceptionexception occurs.
public class Unchecked_Demo
{
public static void main(String args[])
{
int num[] = {1, 2, 3, 4};
System.out.println(num[5]);
}
}
 Output
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 5 at
11 Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)
Prof.A.V.Chechare
12 Prof.A.V.Chechare
Before Exception Handling
Mechanism

 Techniques used
If()
{

}
Else
{

13 Prof.A.V.Chechare
Exception Handling in Java
 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.
 If an exception occurs in your code (suppose in
line 6), then the rest of the code is not executed.
 Therefore Java compiler creates an exception
object and this exception object directly jumps to
the default catch mechanism.
 Where there is a default message which is print
on the screen and our program gets terminated.

14 Prof.A.V.Chechare
JRE Call Stack for Handling
Exception

15 Prof.A.V.Chechare
1. The run-time system searches the call stack to find
the method that contains block of code that can
handle the occurred exception. The block of the
code is called Exception handler.
2. The run-time system starts searching from the
method in which exception occurred, proceeds
through call stack in the reverse order in which
methods were called.
3. If it finds appropriate handler then it passes the
occurred exception to it. Appropriate handler
means the type of the exception object thrown
matches the type of the exception object it can
handle.
4. If run-time system searches all the methods on call
stack and couldn’t have found the appropriate
handler then run-time system handover the
Exception Object to default exception handler ,
which is part of run-time system. This handler prints
16
the exception information in the following format
Prof.A.V.Chechare
and terminates program abnormally.
Java Exception Handling Keywords:
1. try

2. catch

3. finally

4. throw

5. throws

17 Prof.A.V.Chechare
public class TryCatchExample1
{
public static void main(String[] args)
{
int data=50/0;
System.out.println("rest of the code");
}

18 Prof.A.V.Chechare
1.try-catch block
 try:
 Java try block is used to enclose the code that
might throw an exception. It must be used within
the method.
 If an exception occurs at the particular statement
of try block, the rest of the block code will not
execute.
 Java try block must be followed by either catch or
finally block.

19 Prof.A.V.Chechare
Syntax
Try
{
//code that may throw an exception
}
catch(Exception_class_Name ref)
{
}

20 Prof.A.V.Chechare
public class TryCatchExample2
{
public static void main(String[] args)
{
try
{
int data=10/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}

21 Prof.A.V.Chechare
Internal Working of try-catch:

22 Prof.A.V.Chechare
 Examples
1.Array index out of bound
2.File not Found
3.Null pointer
4.String Index Out of Bound
5.Number Format

23 Prof.A.V.Chechare
Multiple catch blocks
Try
{
}
Catch(Exception_type1 obj1)
{
}
Catch(Exception_type1 obj2)
{
}
.
.
.
Catch(Exception obj)
{
}

24 Prof.A.V.Chechare
3.finally
 As we know when exception occurs
 Normal flow of execution is broken
 To handle exception we use try-catch block
 In combination with try catch we can also use
finally block
 Finally block indicates the code which needs to
be executed in any condition
 Code written in finally block will be compulsorily
executed even if exception occurs
 The finally block is executed irrespective of an
exception being raised in the try block. It is
optional to use with a try block.
 example

25 Prof.A.V.Chechare
4.throw
 throw keyword is used to explicitly throw an
exception.
 We can throw either checked or uncheked
exception in java by throw keyword.
 Syntax
throw exception;
 Example
throw new IOException("sorry device error);
Program

26 Prof.A.V.Chechare
throws
 Checked exception (compile time) force you to
handle them, if you don’t handle them then the
program will not compile.
 Throws keyword is used for handling checked
exceptions . By using throws we can declare
multiple exceptions in one go.
 What is the need of having throws keyword when
you can handle exception using try-catch?
Size of code
no of methods

27 Prof.A.V.Chechare
Using Throws
 Declare the exceptions in the method signature
using throws and handle the exceptions where you
are calling this method by using try-catch.
public void myMethod() throws ArithmeticException,
NullPointerException
{
}
public static void main(String args[])
{
try
{
myMethod();
}
catch (ArithmeticException e)
{
28 }
Prof.A.V.Chechare
 example

29 Prof.A.V.Chechare
User Defined Exceptions

30 Prof.A.V.Chechare
31 Prof.A.V.Chechare

You might also like