You are on page 1of 31

CS3391-OBJECT ORIENTED

PROGRAMMING
UNIT 3
Exception Handling and Multithreading

by
S.Sakkaravarthi, AP/IT
Ramco Institute of Technology, Rajapalayam
UNIT 3-TOPICS

 Exception Handling Basics


 Multiple Catch Clauses
 Nested try statements
 Java’s Built-in Exception
 User defined Exception
UNIT 3-TOPICS

 Multithreaded Programming: Java Thread Model


 Creating a thread and multiple threads
 Thread Priorities
 Synchronization-Interthread communication
 Suspending, Resuming and Stopping Threads-Multithreading
 Wrappers-Autoboxing
Exception Handling Basics
Exception is an abnormal event that arises during the
execution of the program and disrupts the normal flow of the
program.

Exception Handling is a mechanism to handle the runtime


errors so that the normal flow of the application can be
maintained.
Exception Handling Basics
There are various types of interruptions while executing any
program like errors, exceptions, and bugs.

These interruptions can be due to programming mistakes or


due to system issues.

Depending on the conditions they can be classified as

1.Exception
2.Errors
Exception

Exceptions are unwanted conditions that disrupt the flow of


the program.
Exceptions usually occur due to the code and can be
recovered.
Exceptions can be of both checked (exceptions that are
checked by the compiler) and unchecked (exceptions that
cannot be checked by the compiler) type.
Exception

They can occur at both compile time and run time.

In Java, exceptions belong to java.lang.Exception class.


Checked Exception

Checked exceptions are those exceptions that are checked at


compile time by the compiler.
The program will not compile if they are not handled.

These exceptions are child classes of the Exception class.

IOException, ClassNotFoundException, SQL Exception are


a few of the checked exceptions in Java.
UnChecked Exception

UnChecked exceptions are those exceptions that are checked


at run time by JVM, as the compiler cannot check
unchecked exception.
The programs with unchecked exceptions get compiled
successfully but they give runtime errors if not handled.
These are child classes of Runtime Exception Class.
UnChecked Exception

Arithmetic Exceptions, NullPointerException,


NumberFormatException, IndexOutOfBoundException are
a few of the unchecked exceptions in Java.
Error
An error is also an unwanted condition but it is caused due
to lack of resources and indicates a serious problem.
Errors are irrecoverable, they cannot be handled by the
programmers. Eg: OutOfMemoryError.
Errors are of unchecked type only.

They can occur only at run time.

In java, errors belong to java.lang.Error class.


Execution Flow
The JVM firstly checks whether the exception is handled or
not. If exception is not handled, JVM provides a default
exception handler that performs the following tasks:
 Print out exception description.
 Prints the stack trace
 Causes the program to terminate
But if the application programmer handles the exception, the
normal flow of the application is maintained, i.e., rest of
the code is executed.
Checked Exception Vs UnChecked Exception
Checked Exception UnChecked Exception
Checked Exception occur at compile time UnChecked Exception Occur at run time

The compiler checks for checked exception The compiler does not check for
unchecked exception

If checked exceptions are not handled, then If unchecked exceptions are not handled,
get compile time error then get run time error
Checked Exceptions are direct subclasses of UnChecked Exceptions are subclasses of
Exception but do not inherit Runtime the Runtime Exception class
Exception class
Example: IOException, Example: ArithmeticException,
ClassNotFoundException, SQLException NullPointerException,
ArrayIndexOutofBoundxception
Java Exception Keywords

Exception handling is managed via five keywords: try, catch, throw,


throws, and finally

 try block: contains the program statements that may raise an


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

 catch block: is used to handle the raised exception. It must be


preceded by try block and can be followed by finally block later.
Java Exception Keywords

 throw keyword: is used to explicitly throw an exception from a


method or any block of code.

 throws keyword: is used to declare that a method may throw a


specific exception.

 finally block: contains statement that must be executed after the


try block. finally block code is used to release resources such as
closing files or database connections.
try-catch block-syntax

try
{
//code that may throw an exception
}
catch(ExceptionClassName ref)
{
//code that may throw an exception
}
try-finally block-syntax

try
{
//code that may throw an exception
}
finally
{
//code
}
finally block
finally block is a block used to execute important code such
closing the database connection.
 Java code within the finally block is always executed
whether an exception is handled or not. Therefore, it
contains all the necessary statements that need to be printed
regardless of the exception occurs or not.
The finally block follows the try-catch block.
Multiple catch clauses
 A try block can be followed by one or more catch blocks.

 Each catch block must contain a different exception handler. Hence multi-
catch block is used to perform different tasks at the occurrence of different
exceptions.

 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 more general

i.e., catch for Arithmetic Exception must come before catch for Exception.
Multiple Catch Block
class Muticatchdemo
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println(“Arithmetic exception occurs:”+e);
}
Multiple Catch Block

catch(ArrayIndexOutofBoundsException e)
{
System.out.println(“Array index out of bounds exception occurs:”+e);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println(“rest of code executes”);
}
}
Multithreading Programming
Multitasking is a process of executing multiple tasks
simultaneously. The concept of multitasking is to utilize the
CPU at its Maximum. Multitasking can be achieved in two
ways
 Process-based Multitasking (Multiprocessing)

 Thread-based Multitasking (Multithreading)


Process

 Process simply means a program in execution.

 Processes have their own separate address space (their own


code & data) & use more resources and hence they are
termed as heavyweight process.
Thread
 Thread is a sequential flow of tasks within a process.
Threads are used to increase the performance of
application.
 Each Thread has its own Program counter, stack and set of
registers. But the threads of a single process might share
the same code and data/file. Threads are also called as
light-weight processes as they share common resources.
References
1. www.scaler.com
2. www.javatpoint.com

You might also like