Escolar Documentos
Profissional Documentos
Cultura Documentos
Because all exceptions that are thrown within a Java program are
objects, grouping or categorization of exceptions is a natural
outcome of the class hierarchy.
StackOverFlowError
Throwable Error UnsatisfiedLinkerError
ArithmeticException
Exception NullPointerException
NegativeArraySizeException
InterruptedException
IndexOutOfBoundsException
RuntimeException
IOException ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
EOFException
FileNotFoundException
Runtime exceptions represent problems that are detected by the
runtime system. This includes arithmetic exceptions (such as
when dividing by zero), pointer exceptions (such as trying to
access an object through a null reference), and indexing
exceptions (such as attempting to access an array element
through an index that is too large or too small).
Runtime exceptions can occur anywhere in a program and in a
typical program can be very numerous.
Typically, the cost of checking for runtime exceptions exceeds the
benefit of catching or specifying them. Thus the compiler does not
require that you catch or specify runtime exceptions, although you
can.
Checked exceptions represent useful information about the
operation of a legally specified request that the caller may have
had no control over and that the caller needs to be informed
about--for example, the file system is now full, or the remote end
has closed the connection.
Examples
class EX
{
public static void main(String args[])
{
int d ,a; try {
System.out.println(After Catch);
}
public static void main(String args[]){
int x=4,y=2,z=0;
try {
z=x/y;
System.out.println("Does not Execute");
}
catch(ArithmeticException e)
{ z=0;
System.out.println(e);
}
finally
{
System.out.println("X= " +x +"Y=" +y+"Z="+z);
}
} /*
finally block is executed if exception is thrown or not */
public static void main(String args[]){
int k=0;
for(int i=0;i<4;i++) {
System.out.println("Case # "+i); try{
switch(i)
{
case 0: int x=4,y=0,z=0; z = x/y; break;
case 1: int b[]=null; k = b[0]; break;
case 2: int c[] = new int[2]; k = c[5]; break;
case 3: "abc".charAt(5); break;
} }
catch(Exception e)
{
System.out.println(e);
}
} // for
}
int k=0;
for(int i=0;i<4;i++) {
System.out.println("Case # "+i); try{
switch(i)
{ case 0: int x=4,y=0,z=0; z = x/y; break;
case 1: int b[]=null; k = b[0]; break;
case 2: int c[] = new int[2]; k = c[5]; break;
case 3: "abc".charAt(5); break;
} }
catch(NullPointerException e) {
System.out.println("It is a null pointer" + e); }
catch(IndexOutOfBoundsException e)
{ System.out.println("Index problem" + e); }
catch(Exception e)
{ System.out.println("Any type of exception" +e); }
} // for
Example for Exceptions are pushed up the Call Stack
public static void method() {
int k=0;
for(int i=0;i<4;i++) { try
{ switch(i)
{
case 3: int x=4,y=0,z=0; z = x/y; break;
case 1: int b[ ]=null; k = b[0]; break;
case 2: int c[ ] = new int[2]; k = c[5]; break;
case 0: "abc".charAt(5); break;
} }
catch(NullPointerException e) {
System.out.println("It is a null pointer" + e); }
catch(IndexOutOfBoundsException e) {
System.out.println("Index problem" + e); }
} // for
} contd
public static void main(String args[])
{
try
{
method();
}
catch(Exception e)
{
System.out.println("Any type of exception" +e);
}
}
Throwing your own Exception
class Ex {
public static void Ticket(int a) throws Exception
{
if(a>60) throw new Exception("Too old ");
else if(a<5) throw new Exception("Too young ");
System.out.println("You
will get the ticket you are" +a);
}
public static void main(String args[]) {
int ages[ ] ={24,3,78,12,4};
for(int i=0;i<ages.length;i++) try
{
Ticket(ages[i]); }
catch(Exception
e){ System.out.println(e); }
} }
Creating Your own Exception Subclasses