Friday 31 August 2012

exception programs in java


exception programs in java:->

//EXCEPTION ZERO

package p2;

public class exc0 {

public exc0() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
int d=2;
try{
int a=42/d;

}catch(ArithmeticException e){
System.out.println("arithmatic exception=");
}finally{
System.out.println("finally =");
}
}

}

//DIVIDE BY EXCEPTION

package p2;

public class Exc1 {
static void subroutine(){
int d=0;
try{
int a=10/d;
}catch(Exception e){
}
}

public Exc1() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Exc1.subroutine();
}

}

//EXCEPTION USING BOTH DIVIDE BY & ZERO

package p2;
public class Exc2 {

public Exc2() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
int d,a;
try{
d=0;
a=42/d;
System.out.println("this wil nt be prntd");
}catch(ArithmeticException e){
System.out.println("division by zero");
}
System.out.println("after catch statement");
}

}

//exception example test

package p2;

import java.io.File;
import java.io.FileInputStream;

public class Extest {
public void doIt(){
File f=new File("C:\\himanshu.doc");
System.out.println(f.exists());
try{
FileInputStream in=new FileInputStream(f);
}
catch(ArithmeticException e){
}
catch(Exception e){
}
}
public static void main(String args[]){
Extest m=new Extest();
m.doIt();
}
}

package p2;

public class getCharDemo {

public getCharDemo() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="this is a demo of get char method";
int start =10;
int end=14;
char buf[]=new char[end-start];
s.getChars(start,end,buf,0);
System.out.println(buf);

}

}

1 comment: