Unit - III
Fundamentals of Exception
The Exception Handling in Java is one of the
powerful mechanism to handle the runtime errors so
that the normal flow of the application can be
maintained.
Exception Handling is a mechanism to handle runtime
errors such as ClassNotFoundException, IOException,
SQLException, RemoteException, etc.
The core advantage of exception handling is to
maintain the normal flow of the application.
An exception normally disrupts the normal flow of
the application; that is why we need to handle
exceptions.
Let's consider a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there are 10 statements in a Java program and
an exception occurs at statement 5; the rest of the code
will not be executed, i.e., statements 6 to 10 will not be
executed.
However, when we perform exception handling, the
rest of the statements will be executed. That is why we
use exception handling in Java.
Checked exceptions are called compile-
time exceptions because these exceptions are checked
at compile-time by the compiler.
The compiler ensures whether the programmer handles
the exception or not. The programmer should have to
handle the exception; otherwise, the system has shown
a compilation error.
The unchecked exceptions are just opposite to
the checked exceptions.
The compiler will not check these exceptions at
compile time.
In simple words, if a program throws an unchecked
exception, and even if we didn't handle or declare it,
the program would not give a compilation error.
Usually, it occurs when the user provides bad data
during the interaction with the program.
Keyword Description
try The "try" keyword is used to specify a block where we should
place an exception code. It means we can't use try block alone.
The try block must be followed by either catch or finally.
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 necessary 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
specifies that there may occur an exception in the
method.
public class JavaExceptionExample
{
public static void main(String args[])
{
try
{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e) {[Link](e);}
//rest code of the program
[Link]("rest of the code...");
}
}
Output
Exception in thread main
[Link]:/ by zero
rest of the code...
In the above example, 100/0 raises an
ArithmeticException which is handled by a try-catch
block.
When an array exceeds to it's size, the
ArrayIndexOutOfBoundsException occurs. there may
be other reasons to occur
ArrayIndexOutOfBoundsException.
Consider the following statements.
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
public class TestFinallyBlock2{
public static void main(String args[]){
try {
[Link]("Inside try block");
//below code throws divide by zero exception
int data=25/0;
[Link](data);
}
//handles the Arithmetic Exception / Divide by zero exception
catch(ArithmeticException e){
[Link]("Exception handled");
[Link](e);
}
//executes regardless of exception occured or not
finally {
[Link]("finally block is always executed");
}
[Link]("rest of the code...");
}
}
public class TestFinallyBlock1{
public static void main(String args[]){
try {
[Link]("Inside the try block");
//below code throws divide by zero exception
int data=25/0;
[Link](data);
}
//cannot handle Arithmetic type exception
//can only accept Null Pointer type exception
catch(NullPointerException e){
[Link](e);
}
//executes regardless of exception occured or not
finally {
[Link]("finally block is always executed");
}
[Link]("rest of the code...");
}
}
Multithreading in Java is a process of
executing multiple threads simultaneously.
A thread is a lightweight sub-process, the
smallest unit of processing. Multiprocessing
and multithreading, both are used to achieve
multitasking.
However, we use multithreading than multiprocessing
because threads use a shared memory area. They don't
allocate separate memory area so saves memory, and
context-switching between the threads takes less time
than process.
Java Multithreading is mostly used in games,
animation, etc.
1) It doesn't block the user because threads are
independent and you can perform multiple operations at
the same time.
2) You can perform many operations together, so it
saves time.
3) Threads are independent, so it doesn't affect other
threads if an exception occurs in a single thread.
A thread is a lightweight subprocess, the smallest unit
of processing. It is a separate path of execution.
Threads are independent. If there occurs exception in
one thread, it doesn't affect other threads. It uses a
shared memory area.
As shown in the above figure, a thread is
executed inside the process.
There is context-switching between the
threads.
There can be multiple processes inside the OS,
and one process can have multiple threads.
In Java, a thread always exists in any one of the
following states. These states are:
New
Active
Blocked / Waiting
Timed Waiting
Terminated
class Multi extends Thread{
public void run(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
[Link]();
}
}
Output:
thread is running...
class Multi3 implements Runnable{
public void run(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
[Link]();
}
}
Output:
thread is running...
public class MyThread1
{
// Main method
public static void main(String argvs[])
{
// creating an object of the Thread class using the constructor Thread(S
tring name)
Thread t= new Thread("My first thread");
// the start() method moves the thread to the active state
[Link]();
// getting the thread name by invoking the getName() method
String str = [Link]();
[Link](str);
}
}
Output:
My first thread