Professional Documents
Culture Documents
Java Iii
Java Iii
DEPARTMENT OF COMPUTATIONAL
STUDIES
UNIT-III
PROBLEM SOLVING USING JAVA
(A20CPT305)
Eg: file not found, arithmetic exception, stack overflow, null pointer, number
format exception
etc.
When we write any program in any programming language, we get the following
errors in types of situations.
i. Compile tim]e occur when you do not follow the syntax of a programming
language
TYPES OF EXCEPTION:
1. Checked exception.
2. Unchecked exception.
3. Error.
Checked exceptions:
Checked exceptions are those exceptional conditions that are checked by compiler at the compile time.
A checked exception forces you to either use try-catch or throws. All exceptions except Error, RuntimeException,
and their subclasses are checked exceptions.
e.g. – IOException, SQLException etc.
Unchecked exceptions:
Unchecked exceptions are those exceptional conditions that are not checked by compiler at the compile time.
Unchecked exceptions are checked at runtime. An unchecked exception not forces you to either use try-catch or
throws. RuntimeException and their subclasses are unchecked exceptions. This Exception can be avoided by
programmer.
e.g. – NullPointerException, ArithmeticException etc.
Error:
Errors are those exceptional conditions that are not checked by compiler at the compile time. Errors are checked at
runtime. An error not forces you to either use try-catch or throws. Error and their subclasses are represents errors.
Error can’t be avoided by programmer, it is irrecoverable.
e.g. – OutOfMemoryError etc.
EXCEPTIONAL HANDLING
MECHANISMS:
USING TRY AND CATCH
The try/catch statement encloses some code and is used to handle errors and
exceptions that might occur in that code. With the help of try and catch we solve
the run time error easy. The general syntax of the try/catch statement is as below:
Example:
MULTIPLE CATCH CLAUSES
When an exception is thrown, each catch statement is inspected in order. The first catch block whose type
matches is executed. After one catch statement executed, the others are bypassed & executed continues after
try-catch block. At that time we use multiple catch
FINALLY
Java support 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. Finally block can be used to handle any exception generated within
a try block.
It may be added immediately after the try block or after the last catch block shown as follow. When a finally block
is defined, this is guaranteed to execute, regardless of whether or not in exception is thrown. As a result, we can
use it to perform certain house-keeping operations such as closing file and releasing system resource.
Syntax:
try
{
// statements
}
catch (<exception> obj)
{
// statements
}
finally
{
//statements
}
DIFFERENCE BETWEEN FINAL, FINALLY, FINALIZE:
• Final:
final is a keyword which can be used to declare anything as constant. Final keyword is utilized in
following ways.
1. Final variable→not to change its value (making variable as constant).
2. Final method→not to override the implementation.
3. Final class→not to extend.
• Finally:
Finally is a clause in try-catch syntax which can be used to execute a block of instructions irrespective of getting
exception in try block, irrespective of getting exception in catch block.
• Finalize:
Finalize is a method of object class which is used for making an object eligible for garbage collection.
THROW
A throw keyword is that which is used to create an exception object explicitly either for predefined or
user defined
In general java applications we get exceptions automatically when we have exception situations in java
app’s
If we want raise an exception explicitly we have to use throw keyword to raise an exception explicitly
by a programmer. We have to use following syntax.
Syntax:
throw new Exception_class([parameter_list]);
CREATING OWN EXCEPTION
In Java, we can create our own exceptions that are derived classes of the Exception class.
Creating our own Exception is known as custom exception or user-defined exception. Basically,
Java custom exceptions are used to customize the exception according to user need.
For creating a thread a class have to extend the Thread Class. For creating a thread by
this procedure you have to follow these steps:
•Override the run( ) method in the subclass from the Thread class to define the code
executed by the thread.
•Create an instance of this subclass. This subclass may call a Thread class constructor
by subclass constructor.
•Invoke the start( ) method on the instance of the class to make the thread eligible for
running.
Implementing the java.lang.Runnable Interface
•A Class implements the Runnable Interface, override the run() method to define the
code executed by thread. An object of this class is Runnable Object.
Types of Synchronization
There are two types of synchronization
1. Process Synchronization
2. Thread Synchronization
Thread Synchronization
There are two types of thread synchronization mutual exclusive and inter-thread communication.
1. Mutual Exclusive
1. Synchronized method.
2. Synchronized block.
3. Static synchronization.
2. Cooperation (Inter-thread communication in java)
Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another while sharing data. It
can be achieved by using the following three ways:
o wait()
o notify()
o notifyAll()
1) wait() method
The wait() method causes current thread to release the lock and wait until either another thread invokes
the notify() method or the notifyAll() method for this object, or a specified amount of time has
elapsed.
The current thread must own this object's monitor, so it must be called from the synchronized method
only otherwise it will throw exception.
2) notify() method
The notify() method wakes up a single thread that is waiting on this object's monitor. If any threads are
waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the
discretion of the implementation.
Syntax:
1. public final void notify()
3) notifyAll() method
Wakes up all threads that are waiting on this object's monitor.
Syntax:
1. public final void notifyAll()
JAVA ENUMS
The Enum in Java is a data type which contains a fixed set of constants.
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, and SATURDAY) , directions (NORTH, SOUTH, EAST, and WEST),
season (SPRING, SUMMER, WINTER, and AUTUMN or FALL), colors (RED, YELLOW,
BLUE, GREEN, WHITE, and BLACK) etc. According to the Java naming conventions, we should
have all constants in capital letters. So, we have enum constants in capital letters.
Java Enums can be thought of as classes which have a fixed set of constants (a variable that does
not change). The Java enum constants are static and final implicitly. It is available since JDK 1.5.
Enums are used to create our own data type like classes. The enum data type (also known as
Enumerated Data Type) is used to define an enum in Java.
AUTOBOXING AND UNBOXING:
The automatic conversion of primitive data types into its equivalent Wrapper type is known as
boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java
programmer doesn't need to write the conversion code.