Professional Documents
Culture Documents
Types of Errors:
1.Compile-Time Error
2.Run-Time Error
1.Compile Time Error: All Syntax errors will be detected and displayed by the java
compiler and therefore these errors are known as compile time errors. Whenever the
compiler displays an error, it will not create the .class file.
Eg:
• Missing Semicolons
• Mismatch of brackets in the class and methods
• Misspelling the identifiers and keywords
• Use of undeclared variables
2.Run-Time Error:
A Program may compile successfully creating the .class file but
may not run properly. Such programs may produce wrong result due to wrong logic
or may terminate due to errors
Eg:
• Dividing an integer by zero
• Accessing an element that is out of bounds of an array
• Converting invalid string to a number
• Passing parameter that is not in a valid range
Exceptions
• An exception is an abnormal condition that is caused by a run-time error in the
program.
• The way of handling the various types of exceptions that occurs during the
execution of the program is known as exception handling.
• Some of the error classes are described below:
Syntax
Throws Exception
Exception Handler
Object
Eg:
try
{
statement; //generates exception
}
catch(Exception-type e)
{
Srimad Andavan Arts and Science College
Department of Computer Sciecne
statement; //process the exception
}
catch(Exception-type-1 e)
{
statement; //process the exception type-1
}
catch(Exception-type-2 e)
{
statement; //process the exception type-2
}
catch(Exception-type-3 e)
{
statement; //process the exception type-3
}
…..
……
catch(Exception-type-N e)
{
statement; //process the exception type-N
}
• When an exception in a try block is generated, the java compiler treats the multiple
catch statements like cases in switch statement.
Nested Try:
1. The try statement can be nested. A try statement inside the block of another try
is called nested try.
2. If an inner try statement does not have a catch handler for a particular exception,
then it is unknowned exception and the next try statement’s catch handler are
matched.
3. If no catch statements matches, then the java run-time system will handle the
exception.
Syntax:
try
{
……
……….
try
{
………
………..
}
catch(…..)
{
………
………….
}
}
catch(………)
{
………..
…………..
}
Finally Statement
➢ Java supports another statement known as finally statement that can be used to
handle an exception that is not caught by any of the previous catch statements.
try try
{ {
…… ……
……… ………
} }
finally Or catch()
{ {
……… ………
………… …………
} }
finally
{
………
…………
}
• When a finally block is defined, this will execute whether or not in exception
thrown.
Throw
• throw keyword is used to throw our own Exception.
Syntax:
throw new Throwable_subclass;
• Here throwable_subclass must be an object of type Throwable or a subclass
of Throwable.
• There are two ways of obtaining a Throwable object,
1. Using a parameter into a catch clause or
2. Creating one with the new operator.
• The flow of execution stops immediately after the throw statement.
Throws:
• If a method is capable of causing an exception that it does not handle, it must
specify the behavior of guard the exception. A throws clause is used to handle
this.
• A throws clause lists the type of exceptions that a method might throw.
• The exceptions that a method can throw must be declared in the throws clause.
Multithreading
Thread
A thread is a program unit that is executed independently of other parts of the
program.
Life Cycle of a Thread
Every thread moves through several states from its creation to its
termination. The possible states of a thread are, New, Ready(Runnable), Running, Waiting
and Dead
New State
Running
Dead
Wait
The Thread will be in the new state after the creation of the thread. After the start()
method of the thread is executed, it will move to the ready state. When the JVM selects it for
execution, it will move to the running state. When the thread completes its execution ,it will
move to the dead state.
Srimad Andavan Arts and Science College
Department of Computer Sciecne
If a thread is instructed to wait, it moves to the waiting state. when the waiting is over
,the thread once again moves to the ready state. Sometimes,the JVM may move a thread from
the running state to the ready state in order to offer another thread to execute.
NewBorn State
When we create thread object, the thread is born and is said to be in
newborn state. At this state we can perform the following two methods start() and stop().
Runnable State
The runnable state means that the thread is ready for execution and
is waiting for the availability of the processor. if all threads that are waiting for execution has
equal priority, then they are given time slots for execution. The yield() method is used to
relinquish the control to other method.
Running State
The Running state means that the processor has given its time to the
thread for its execution. the running thread may block its control by the following
methods,
1. It has been suspended by suspend() method. Then it can be revived by using the
resume() method.
2. It has been made to sleep by using sleep(time) method.
3. It has been said to wait until some event occurs by wait() method and the thread
can be run again using notify() method.
Blocked State
A thread is said to be blocked when it is prevented from entering into
the runnable state and the running state. This happens when a thread is suspended, wait or
sleeping state.
Dead State
A running thread ends its life when it has completed its run() method.