0% found this document useful (0 votes)
29 views

Exception Handling

The document discusses exceptions in Java. It provides examples of different types of exceptions like ArithmeticException, ArrayIndexOutOfBoundsException, and various checked and unchecked exceptions. It explains how exceptions are handled and propagated in Java code through try/catch blocks and method declarations that throw specific exception types. The examples demonstrate different scenarios of throwing and catching exceptions.

Uploaded by

puvi ththira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Exception Handling

The document discusses exceptions in Java. It provides examples of different types of exceptions like ArithmeticException, ArrayIndexOutOfBoundsException, and various checked and unchecked exceptions. It explains how exceptions are handled and propagated in Java code through try/catch blocks and method declarations that throw specific exception types. The examples demonstrate different scenarios of throwing and catching exceptions.

Uploaded by

puvi ththira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Madhuranga Wijesekara

Copyrights 2016 SimpleSoft inc.


All rights reserved.
§ An exception is a way that the java language says the developer about the
problems that occurs in the OS/some hardware part or in the application during
the Run-time.

§ Java language does inform these exceptions as in the form of an Object.

§ But we know in order to create an object there should be specific class for that.

§ For that purpose Java language provide us very rich set of Exception Classes.
(01) (02)
class A{ class A{
public static void main(String args[]){ public static void main(String args[]){
int x=1+2; int x=2/0;
System.out.println(x); System.out.println(x);
} }
} }

(03) (04)
class A{ class A{
int x; public static void main(String args[]){
public static void main(String args[]){ String s=“ABC”;
A t=null; System.out.println(s.charAt(29));
System.out.println(t.x); }
} }
}
(05)
class A{
public static void main(String args[]){
try{
int x=2/0;
System.out.println(x);

}catch(Exception e){
System.out.println(e);
}
}
}
throw new ……………………………();

An Exception Class

Examples
Ø throw new ArithmaticException();
Ø throw new ArrayIndexOutOfBound();
Ø throw new ClassCastException();
Ø throw new InterruptedException();
Ø throw new ClassNotFoundException();
Ø throw new IlegalAccessException();
CHECKED UNCHECKED
§ If the compiler knows an exception will get thrown froma particular
statement at the compile-time, then the compiler will check whether
that statement causes an checked exception or an unchecked
exception.
§ If that statement throw an
checked exception(compile-time
exception), handling that is compulsory, otherwise an compile
error(unreported exception) will arise.
§ If that statement throw an
unchecked exception(run-time exception),
handling that is not compulsory. It wont causes and compile time
error.
{compile-time exception}

throw

{run-time exception}
throw new Exception();
(07)
(06) class A{
public static void main(String args[]){
class A{
boolean b=2>1;
public static void main(String args[]){
if(b){
throw new ArithmaticException();
throw new Exception();
int x=0;
}
}
int x=0;
}
System.out.println(x);

}
}
(08) (09)
class A{ class A{

public static void main(String args[]){ public static void main(String args[]){
boolean b=2>1;
boolean b=2>1;
if(b){
if(b){
throw new ArithmaticException();
throw new ArithmaticException();
}else{
}
throw new ArithmaticException();
int x=0;
}
System.out.println(x); int x=0;
System.out.println(x);
}
}
}
}
(11)
(10) class A{
class A{ public static void main(String args[]){
public static void main(String args[]){ boolean b=2>1;
boolean b=2>1; if(b){
if(b){
try{
try{
int x=0;
throw new ArithmaticException();
}catch(Exception e){} System.out.println(2/x);
}else{ }catch(Exception e){
throw new ArithmaticException(); System.out.println(x+” is 0”);
} }
int x=0; }else{
System.out.println(x);
throw new ArithmaticException();
}
}
}
int x=0;
System.out.println(x);
}
}
(12)
class A{
public static void main(String args[]){
m( );
}

static void m( )throws Exception{

}
}
(13)
class A{
public static void main(String args[]){
System.out.println(“start”);
if(true){
throw new RuntimeException();
}else{
throw new ArithmaticException();
}
System.out.println(“end”);
}
}
(14)
class A{
public static void main(String args[]){
System.out.println(“start”);
if(true){
throw new RuntimeException();
}else{
int x=2/0;
}
System.out.println(“end”);
}
}
(15)

class A{
public static void main(String args[]){
m( );
}
static void m( )throws Exception{
throw new Exception();
}
}
(16)

class A{
public static void main(String args[]){
m( );
}
static void m( )throws Exception{
System.out.println(“Hello Will I Be Printed!”);

}
}
(17)

class A{
public static void main(String args[]){
m( );
}
static void m( )throws RuntimeException{
System.out.println(“ABC”);
}
}
(18)

class A{
public static void main(String args[]){
System.out.println(“ABC”);
m( );
System.out.println(“XYZ”);
}
static void m( )throws RuntimeException{
System.out.println(“Hello”);
}
}
(19)

class A{
public static void main(String args[]){
System.out.println(“ABC”);
m( );
System.out.println(“XYZ”);
}
static void m( )throws Exception{
throw new Exception( );
}
}
(20)
class A{
public static void main(String args[]){
System.out.println(“ABC”);
if(true)
m( );
else
throw new RuntimeException( );
System.out.println(“XYZ”);
}
static void m( )throws RuntimeException{
throw new RuntimeException( );
}
}
(21)
class A{
public static void main(String args[]){
System.out.println(“start”);
if(true)
m( );
else
throw new RuntimeException( );
System.out.println(“XYZ”);
}
static void m( )throws Exception{
throw new RuntimeException( );
}
}
(22)

class A{
public static void main(String args[]){
try{
m( );
}catch(Exception e){}
}
static void m( )throws Exception{
throw new InterruptedException( );
}
}
(23)

class A{
public static void main (String args[])throws Exception{
m( );
}
static void m( )throws Exception{
throw new InterruptedException( );
}
}
(24)

class A{
public static void main (String args[])throws Exception{
m( );
}
static void m( )throws InterruptedException,Exception{
throw new Exception( );
}
}
(25)
class A{
public static void main (String args[]) {
System.out.println(“a”);
m( );
System.out.println(“b”);
}
static void m( ){
throw new RuntimeException( );
}
}
(26)

class A{
public static void main (String args[]) {
System.out.println(“a”);
try{
m( );
}catch(Exception e){}
System.out.println(“b”);
}
static void m( ){
throw new RuntimeException( );
}
(27)

class A{
public static void main (String args[]) {
if(true)
m( );
else
throw new ArithmaticException( );

System.out.println(“End main”);
}
static void m( )throws Exception{
System.out.println(“First”);
throw new RuntimeException( );
System.out.println(“End”);
}
}
(28)

class A{
public static void main (String args[]) {
if(true)
m( );
else
throw new ArithmaticException( );

System.out.println(“End main”);
}
static void m( )throws RuntimeException{
System.out.println(“First”);
throw new RuntimeException( );
}
}
(29)

class A{
public static void main (String args[])throws Exception{
System.out.println(“B4”);
throw new Exception();
System.out.println(“After”);
}
}
(30)

class A{
public static void main (String args[]) {
m();
}
static int m( )throws RuntimeException{
System.out.println(“First”);
throw new RuntimeException( );
}
}
(31)

class A{
public static void main (String args[])throws Exception {
System.out.println(“B4”);
if(true){
throw new Exception( );
}
System.out.println(“After”);
}
}
try{ //code1 }
catch( ){ //code2 }
.
.
.
finally{ //code3}
(32)
class A{
public static void main(String args[]){
try{
throw new Exception( );
}catch(Exception e){
System.out.println(“abc”);
}
}
}
(33)
class A{
public static void main(String args[]){
try{
throw new Exception( );
}
}
}
(34)
class A{
public static void main(String args[]){
try{
System.out.println(“abc”);
}finally{
System.out.println(“xyz”);
}
}
}
(35)
class A{
public static void main(String args[]){
try{
throw new Exception( );
}finally{
System.out.println(“xyz”);
}
}
}
(36)
class A{
public static void main(String args[]){
try{
System.out.println(“start”);
throw new Exception( );
System.out.println(“end”);
}catch(Exception e){
System.out.println(“exception”);
}finally{ System.out.println(“xyz”); }
}
}
(37)
class A{
public static void main(String args[]){
try{
System.out.println(“start”);
int x=1/0;
System.out.println(“end”);
}catch(Exception e){
System.out.println(“exception”);
}finally{ System.out.println(“xyz”); }
}
}
(38)
class A{
public static void main(String args[]){
try{
System.out.println(“start”);
int x=1/0;
System.out.println(“end”);
} finally{
System.out.println(“xyz”);
}
}
}
(39)
class A{
public static void main(String args[]){
try{
throw new ArithmaticException( );
}catch(ArithmaticException e){
System.out.println(“a”);
} catch(RuntimeException e){
System.out.println(“b”);
} catch(Exception e){
System.out.println(“c”);
}
}
}
(40)
class A{
public static void main(String args[]){
try{
throw new ArithmaticException( );
}catch(RuntimeException e){
System.out.println(“a”);
} catch(ArithmaticException e){
System.out.println(“b”);
} catch(Exception e){
System.out.println(“c”);
}
}
}
(41)
class A{
public static void main(String args[]){
try{
System.out.println(“Hello”);

}catch(InterruptedException e){
System.out.println(“a”);
}
}
}
(42)
class A{
public static void main(String args[]){
try{
int x=1/0;
}catch(InterruptedException e){
System.out.println(“a”);
}
}
}
(43)
class A{
public static void main(String args[]){
try{
System.out.println(“AAA”);
}catch(Exception e){
System.out.println(“EEE”);
}
}
}
(44)
class A{
public static void main(String args[]){
try{

}catch(InterruptedException e){

}
}
}
(45)
class A{
public static void main(String args[]){
int x=0,d=0;
try{
d=42/0;
}catch(ArithmaticException e){
System.out.println(“Arithmatic Error”);
} catch(ArrayIndexOutOfBoundException e){
System.out.println(“Array Index Error”);
} finally{
System.out.println(“finally”);
}
System.out.println(“After”);
}
}
(46)
class A{
public static void main(String args[]){
}
void m( )throws InterruptedException{
}
}
Class B extends A{
void m()throws Exception{
}
}
(47)
class E1 extends Exception{ }
class E2 extends E1{ }
Class A{
void m( )throws E1{}
}
class B extends A{
protected void m( )throws Exception{}
public static void main(String args[]){
A a=new A( );
a.m();
}
}
(48)
class A{
public static void main(String args[])throws Exception{
throw new Exception(“ABC” );
}
}
(49)
class A{
public static void main(String args[])throws Exception{
throw new E( “ABC”);
}
}

class E extends Exception{

}
(50)
class A{
public static void main(String args[])throws Exception{
throw new E( “ABC”);
}
}

class E extends Exception{


E(String s){
super(s);
}
}
(51)
class A{
public static void main(String args[]) {
System.out.println(“Start”);
m( );
System.out.println(“end”);
}
static void m( )throws E{
try{ throw new RuntimeException(); }
catch(ArithmaticException ae){ System.out.println(“exception…”);}
finally{System.out.println(“Finally…”);}
}
}
class E extends RuntimeException{ }
(52)
class A{
public static void main(String args[]) {
try{

}catch(E e){

}
}
}
class E extends RuntimeException{ }
(53)
class A{
static void m( )throws Exception{ }
public static void main(String args[]) {
try{
m( );
}finally{
System.out.println(“Finally”);
}
}
}
(54)
class A{
public static void main(String args[]){
int array[]=new int[5];
try{
array[5]=85;
}catch(ArithmaticException e){
System.out.println(“Arithmatic Error”);
} finally {
System.out.println(“finally”);
} catch(Exception e){
System.out.println(“exception”);
}
System.out.println(“After”);
}
}
(55)
class A{
public static void main(String args[]){
int array[]=new int[5];
try{
array[5]=85;
}catch(ArithmaticException e){
System.out.println(“Arithmatic Error”);
} catch(Exception e){
System.out.println(“exception”);
} catch(Exception e){
System.out.println(“exception2”);
} finally{System.out.println(“Successful!”);}
System.out.println(“After”);
}

You might also like