U4 Exception Handling and multithreadinng part1
U4 Exception Handling and multithreadinng part1
Multithreading
1
Introduction
•
An Error indicates serious problem that a
reasonable application should not try to catch or
that application may crash.
•
An Exception is an unwanted event that interrupts
the normal flow of the program. When an exception
occurs program execution gets terminated.
2
ATM Machine (Scenario 1)
3
ATM Machine (Scenario 2)
4
Exception Hierarchy
7
• For example, if you use class in your
program to read data from a file, if the file
specified in its constructor doesn't exist, then a
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
• 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
• -
1. An unchecked exception is an exception that
occurs at the time of execution.
10
• 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
ExcPeropf.tAi.oV.n
Chs
ec.U
han
rechecked_Demo.main(Unchecked_Demo.java:8)
12
Before Exception Handling
Mechanism
• Techniques used
If()
{
}
Else
{
13
Exception Handling in Java
• The 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
JRE Call Stack for Handling
Exception
15
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 .
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 ,
which is part of run-time system. This handler prints
16
tPhroef.A.eV.xChcecehapretion information in the following format
and terminates program .
17
TryCatchExample1
{
main(String[] args)
{
data=50/0; System.out.println("rest
of the code");
}
18
1.try-catch block
• try:
• Java 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
Syntax
{
//code that may throw an exception
}
(Exception_class_Name ref)
{
}
20
TryCatchExample2
{
main(String[] args)
{
{
data=10/0;
}
(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
21
Internal Working of try-catch:
22
• Examples
1. Array index out of bound
2. File not Found
3. Null pointer
4. String Index Out of Bound
5. Number Format
23
Multiple catch blocks
Try
{
}
Catch(Exception_type1 obj1)
{
}
Catch(Exception_type1 obj2)
{
}
.
.
.
Catch(Exception obj)
{
}
24
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
in the try block. It is
to use with a try block.
• example
25
4.throw
• keyword is used to explicitly throw an
exception.
• We can throw either checked or uncheked
exception in java by throw keyword.
• Syntax
new ExceptionType("Error message");
• Example
IOException("sorry device error);
Program
26
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)
{
27
28 }Prof.A.V.Chechare