Exception Handling and Collections
Exception Handling and Collections
Exception Handling
What is Exception in Java?
In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
Types of Exception
1) Checked Exception: The classes which directly inherit Throwable class except RuntimeException and Error are known as
checked exceptions like IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception: The classes which inherit RuntimeException are known as unchecked exceptions like
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at
compile-time, but they are checked at runtime.
Example:
try {
catch(FileNotFoundException e) {
Runtime Stack Mechanism: For every thread JVM will create a separate stack at the time of Thread creation. All method calls performed by that thread will be
stored in that stack. Each entry in the stack is called "Activation record" (or) "stack frame". After completing every method call JVM removes the corresponding
entry from the stack. After completing all method calls JVM destroys the empty stack and terminates the program normally.
Customized Exception Handling by using try-catch:
try
{
Risky code
}
catch(Exception e)
{
Handling code
}
Try with multiple catch blocks:
The way of handling an exception is varied from exception to exception. Hence for every exception type it is recommended to take a separate catch block. That is try with multiple
catch blocks is possible and recommended to use.
try
{
.
.
.
.
catch(FileNotFoundException e)
{
use local file
}
catch(ArithmeticException e)
{
perform these Arithmetic operations
}
catch(SQLException e)
{
try
{
risky code
}
catch(x e)
{
handling code
}
finally
{
cleanup code
}
The speciality of finally block is it will be executed always irrespective of whether the exception raised or not raised and whether handled or not handled.
Difference between final, finally, and finalize:
final:
finally is the block always associated with try-catch to maintain clean up code which should be executed always
irrespective of whether exception raised or not raised and whether handled or not handled.
finalize:
finalize is a method, always invoked by Garbage Collector just before destroying an object to perform cleanup
activities.
throw statement:
Sometimes we can create Exception object explicitly and we can hand over to the JVM manually by using throw keyword.
Example:
class Test
{
public static void main(String[]
args){
throw new ArithmeticException("/
by zero");
}}
Collection
1. If we want to represent a group of "individual objects" as a single entity then we should go for
collection.
2. In general we can consider collection as root interface of entire collection framework.
3. Collection interface defines the most common methods which can be applicable for any collection
object.
4. There is no concrete class which implements Collection interface directly.
9(Nine) key interfaces of collection framework:
1. Collection
2. List
3. Set
4. SortedSet
5. NavigableSet
6. Queue
7. Map
8. SortedMap
9. NavigableMap
List Interface
List:
ArrayList:
SortedSet:
NavigableSet:
Queue:
If we want to get objects one by one from the collection then we should go for cursor. There are 3 types of
cursors available in java. They are:
1. Enumeration
2. Iterator
ListIterator
Enumeration:
1. We can use Enumeration to get objects one by one from the legacy collection objects.
2. We can create Enumeration object by using elements() method. public Enumeration
elements();
Enumeration e=v.elements(); using Vector Object
Iterator
Iterator:
1. We can use Iterator to get objects one by one from any collection object.
2. We can apply Iterator concept for any collection object and it is a universal cursor.
3. While iterating the objects by Iterator we can perform both read and remove operations.
ListIterator: