8 - Exception Handling, Collection
8 - Exception Handling, Collection
What is an Exception?
• It’s an abnormal condition that occurs during the execution of a
program, & due to which the normal flow of the program's
instructions may disrupt.
For example:-
class Test{
public static void main(String[] args){
System.out.println(5/0);
System.out.println("Hello");
// do something else
}
}
Guess the output…
Exception in thread "main" java.lang.ArithmeticException: / by zero
at NewClass.main(NewClass.java:20)
Throwing an exception
• When an error occurs within a method, the run-time
system creates an object and hands it off to the method.
The object, called an exception object, contains
information about the error, including its type and the state
of the program when the error occurred. Creating an
exception object and handing it to the relevant method is
called throwing an exception.
• Checked Exception
• Run-time Exception
Unchecked Exception
• Error
Class Hierarchy
java.lang.Object
java.lang.Throwable
java.lang.Error java.lang.Exception
java.lang.RuntimeException
Checked Exception
• An exceptional condition that a well-written application
should anticipate and recover from. For example:-
• java.io.FileNotFoundException
• Java.io.IOException
• Java.io.InterruptedException
• Ex :- java.io.IOError
java.lang.Object
ArrayIndexOutOfBoundsException
java.lang.Throwable
StringIndexOutOfBoundsException
java.lang.Exception
IndexOutOfBoundsException
java.lang.RuntimeException
int arr[] = {5,6,7};
try{
System.out.println(arr[5]);
}
catch(Exception e)
{
System.out.println("first catch");
}
catch(RuntimeException re)
{
System.out.println("2nd catch");
}
public static void main(String[] args) {
System.out.println(m());
}
static int m() {
try {
int x = 7/0;
return x;
}
catch(Exception e) {
System.out.println("catch block");
return 0;
}
finally {
System.out.println("finally block");
return 1;
}
}
Handling Checked Exception
public static void main(String[] args) throws FileNotFoundException
{
try{
m();
}
catch(IOException e)
{ //….. }
}
static void m() throws FileNotFoundException
{
try{
FileInputStream fis = new FileInputStream("abc.java");
}
catch(FileNotFoundException
e)
{ //….. }
}
Keywords For Exception Handling:
• try
• catch
• finally
• throws
• throw
20
Collection Framework
Supported By : java.util package
Introduction
Collections Framework provides a well-designed set of interfaces
and classes for storing and manipulating groups of data as a single
unit, a collection.
java.util.Map<E>
java.util.Map<E>
java.util.Collection<E>
java.util.Collection<E>
java.util.SortedMap<E>
java.util.SortedMap<E>
java.util.List<E> java.util.Set<E>
java.util.Set<E>
java.util.List<E>
java.util.Queue<E> java.util.NavigableMap<E>
java.util.NavigableMap<E>
java.util.Queue<E>
Added in Java SE 6
java.util.SortedSet<E>
java.util.SortedSet<E>
Added in Java SE 6
java.util.Deque<E>
java.util.Deque<E>
java.util.NavigableSet<E>
java.util.NavigableSet<E>
Added in Java SE 6
java.util.Collection<E>
• Super Interface for all classes that define a collection.
boolean isEmpty()
Iterator iterator()
Object[] toArray()
boolean remove(Object)
java.util.Collection<E> contd.
boolean containsAll(Collection c)
boolean addAll(Collection c)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
void clear( )
java.util.Iterable<E>
java.util.Iterable<E>
java.util.Collection<E>
java.util.Collection<E>
java.util.List<E>
java.util.List<E>
java.util.List<E> contd.
ListIterator listIterator()
Object remove(int)
Object set(int)
Collection<E>
Collection<E>
AbstractCollection<E>
AbstractCollection<E>
List<E>
List<E>
AbstractList<E>
AbstractList<E>
ArrayList<E>
ArrayList<E>
boolean hasNext();
Object next();
boolean hasPrevious();
Object previous();
int nextIndex();
int previousIndex();
void remove();
void add(Object);
Using for-each style for loop
ArrayList names = new ArrayList();
names.add("John");
names.add("Brad");
names.add("Smith");
for(Object name:names){
// your code goes here
}
Set<E> extends Collection<E>
Declares the behavior of a Set. i.e., a Collection that doesn’t allow
duplicate elements.
E first()
HashSet<E> & TreeSet<E>
Java.lang.Object
Java.lang.Object
AbstractCollection<E>
AbstractCollection<E>
Set<E>
Set<E> AbstractSet<E> NavigableSet<E>
NavigableSet<E>
AbstractSet<E>
HashSet<E>
HashSet<E> TreeSet<E>
TreeSet<E>
Using TreeSet<E>
TreeSet countries = new TreeSet();
countries.add("India");
countries.add("America");
countries.add("France");
countries.add("London");
countries.add("Russia");
countries.add("China");
System.out.println(countries);
• void clear( )
• boolean containsKey(Object k)
• boolean containsValue(Object v)
• Set<Map.Entry<K,V>> entrySet( )
• V get(Object key)
boolean isEmpty( ) Map<K,V> contd.
Set<K> keySet( )
V put(K,V)
Adds a key-value pair. Overwrites any previous value associated with the
key. Returns null if key didn’t already exist. Otherwise previous value with the
key is returned.
V remove(Object key)
int size( )
Collection<V> values( )
HashMap<K,V> & TreeMap<K,V>
Map<K,V>
Map<K,V>
Java.lang.Object
Java.lang.Object
SortedMap<K,V>
SortedMap<K,V>
AbstractMap<K,V>
AbstractMap<K,V>
NavigableMap<K,V>
NavigableMap<K,V>
HashMap<K,V>
HashMap<K,V> TreeMap<K,V>
TreeMap<K,V>
HashMap<K,V>
Uses HashTable to store the map.
The insertion and retrieval time is independent of the size of the map.
Constructors
HashMap()
valueOf() methods
Integer i1 = Integer.valueOf(“101”,2);
Integer i2 = Integer.valueOf(“F”,16);
Integer i3 = Integer.valueOf(“012”,8);
Integer i4 = Integer.valueOf(“102”);
Class Hierarchy
java.lang.Object
java.lang.Number
java.lang.Character
java.lang.Byte
java.lang.Boolean
java.lang.Short
java.lang.Float java.lang.Integer
java.lang.Double java.lang.Long
java.lang.Number
public java.lang.Number();
java.lang.Object
f
Boxing & Unboxing
• int a = ref.intValue() + 5;
Getting the encapsulated value out of the object is
called unboxing or unwrapping.
AutoBoxing & AutoUnboxing
Supported by jdk1.5+
• Integer i = 67;
automatically translated into following code by the compiler.
Integer i = new Integer(67); // so called AutoBoxing
• int a = i + 10;
automatically translated into following code by the compiler.
int a = i.intValue() + 10; // so called AutoUnboxing