[go: up one dir, main page]

0% found this document useful (0 votes)
29 views22 pages

OOP 1 - Unit 4

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)
29 views22 pages

OOP 1 - Unit 4

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/ 22

UNIT 4

What is Exception
An exception is unexpected, unwanted, abnormal situation that occur
at runtime is call exception.

What is Exception Handling


Exception handling is the process of handling to unwanted or unexpected
events when a computer program runs.
In exception handling, we should have an alternate source through which
we can handle the exception.
This mechanism provides four following steps to handle
the exception.
1). Find the problem. (Hit the exception)
2). Inform that an error has occurred. (Throw the
exception)
3). Receive the error information. (Catch the exception)
4). Take corrective actions (Handle the exception)
Handle an exception
The object oriented mechanism has provides the following technique to
work with exception.
All are reserved keywords.

try catch throw throws finally


Exception Handling
During developing of any program we make some types of errors. Such
as…
There are mainly 2 types of error in JAVA.

Compile time errors


Runtime errors
What is compile time error?
Any syntax is incorrect in our program that time compiler generates
errors are known as compile time errors.
Such as missing semicolons.
Exception Handling
What is runtime error?
When we execute any program that time any errors occur is known as
runtime errors.
It is also known as Exception.
Some examples of Exception are…..
- Division by zero
- Stack overflow
- Null pointer assignment
- Arithmetic overflow
- Out-of-bounds array subscript
Try Block

Code that is likely to generate an exception (error) is enclosed or written


in a try block.
In other words we can say statements which may be generate an
exception is written in try block.
Syntax:
try
{
//Code that might causes an error
}
Catch Block

Catch statement is passed a single parameter, which is reference to the


exception object thrown by the try block.
If the catch parameter matches with the type of exception object, then the
exception is caught and the statements in the catch block will be
executed.
Syntax:
catch ( Execption-type e )
{
Statement for managing exception
}
Multiple Catch Block

When statements in a single try block generate multiple exceptions, we


require multiple catch blocks to handle different types of exceptions.
Syntax:
try
{
//Code that might causes an error
}
catch ( ArithmeticException e)
{
Statement for managing arithmetic exception.
}
catch ( NullPointerException e )
{
Statement for managing null pointer exception
}
Throw Keyword
Throw keyword is used to throw the user defined or customized
exception object to the JVM explicitly for that purpose we use throw
keyword.

Throws Keyword
The Java throws keyword is used to declare an exception. It gives an
information to the programmer that there may occur an exception.
Finally Block

Finally block is a real time block and the main purpose of finally block
to handle the resources.
Finally block is always execute
if there is an exception occur or not occur.

Finally block is a real time block and the main purpose of finally block
to handle the resources.
Exception Hierarchy

Throwable class is the super class or root class of java exception


hierarchy which contains two sub class.

1. Exception
2. Error
Throwable

Exception Error

1. RuntimeException 1. StackOverflowError
2. IO Exception 2. OutOfMemoryError
3. SQL Exception 3. IOError
4. Interrupted Exception 4. LinkageError
5. ClassNotFouneException
6. Etc...
Exception

There are two types of exception.


Checked Exception Unchecked Exception
The exception which are checked by compiler The exception which are not checked by
for smooth execution of program at runtime. compiler and it directly taken care by JVM.
Checked exception are commonly occurred Unchecked exception are rarely occur
exception so, the compiler takes very much care exception so, compiler doesn’t take very much
about these exception. care about those exception.

Home Work
Checked and unchecked exception hierarchy.
Multithreading

Multithreading
Multithreading is a process to execute multiple threads at the same time
without dependency of other threads called multithreading.

Thread
Thread is a lightweight process, It is a pre defined class in java which is
available in java.lang package.
Thread is a basic unit of CPU and it is well known for independent
execution.
A thread is an independent path of execution in java.
Thread is a line of execution in a program.
A thread in java helps to achieve multithreading in java.
The main method is also knows as the main thread of execution.
The JVM or java virtual machine allows an application to execute
multiple threads within a program concurrently.
When we start the JVM, a single thread (main) started by main( )
method.
How to create Thread in JAVA?

We can create thread in java by using two ways.

1. By extending Thread Class

2. By implementing Runnable Interface

Note: Thread class is pre defined class.


Runnable interface is pre define interface.
Create thread by using extending thread class.

class A extends Thread class ThreadDemo


{ {
public void run() p.s.v.m(S ar[])
{ {
// code A t = new A();
} t.start();
} }
}

run() and start() method is pre –define methods in Thread class

sleep( ), join( ), yield( ), suspend( ), resume( ).


Create thread by using Implementing Runnable Interface
class A implements Runnable class ThreadDemo
{ {
public void run() p.s.v.m(S ar[])
{ {
// code A ob = new A();
} Thread t = new Thread(ob)
}
t.start( );
}
}
In Runnable interface there is only one method. That is run( ).

To get name: Thread.currentThread().getName()


Thread Priority

In java it is possible to assign the priority of thread.


To set these priority java thread class has provide two pre
define method.
1. setPriority()
2. getPriority()
The thread class has also provided three pre-define final static
variable and it’s value lie between 1 to 10.
1) Thread . MIN_PRIORITY (1)
2) Thread . NORM_PRIORITY (5) (By Default)
3) Thread . MAX_PRIORITY (10)
To get priority: Thread.currentThread().getPriority()
Thread Life Cycle

In Java, a thread always exists in any one of the following


states. These states are:
1) New State (Born)
2) Runnable State (Active/Ready)
3) Running State (Execution)
4) Waiting State (Blocked)
5) Dead State (Exit/Terminate)
Thread Life Cycle

Waiting

t.start( ) TS
Born Runnable Running Dead
t.stop( )

T S = Thread Scheduler, follow some algorithm for thread execute.

You might also like