ArrayIndex out of bound Exception

0

/**
 *
 * @author vishnu
 */
import java.util.*;
class Array{
    int size;
    int pointer;
    int[] arr;
    
    Array(int s,int initialValue){
        arr=new int[s];
         set(initialValue);
    }
    void set(int i){
        Arrays.fill(arr,i);
       
    }
    int getLength(){
        return arr.length;
    }
    void sort(){
        Arrays.sort(arr);
    }
    void getValue(int index){
        try{
            System.out.println(arr[index]);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}
public class ArrayExcep {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Array A=new Array(5,0);
        A.getValue(2);
        A.getValue(7);//accessing an invalid index
    }
}

Number format Exception-Java

0

//the following program throws a Number format exception when an invalid hexadecimal string is given as input
/**
*
* @author vishnu
*/
import java.io.*;
import java.lang.*;
import java.util.Scanner;

public class HexString {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
Scanner S=new Scanner(System.in);
System.out.println(“Enter the Hexadecimal number:”);
String str= S.next();
int i= Integer.parseInt(str,16);
System.out.println(“Decimal:=”+ i);
}
}

Creating custom Exceptions -Java

0

Here is a program which throws a custom exception when a negative value is entered for radius
/**
*
* @author vishnu
*/
import java.util.*;

class Circle{
private double radius,area;
Circle(){
radius=0.0;
area=0.0;

}
void setRadius(){
System.out.println(“enter radius”);
Scanner input=new Scanner(System.in);
radius=input.nextDouble();
try{
if(radius<0)
throw new CustomException(“wrong value of radius”);
area=3.14*radius*radius;
}
catch(CustomException e){
System.out.println(“message”+e.getMessage());
}

}
void show(){
System.out.println(“area=”+area);
}
}
class CustomException extends Exception {
private String message= null;
public CustomException(){
super();
}
public CustomException(String message){
super(message);
this.message=message;
}
}
public class CircleTest{
public static void main(String args[]){
Circle cob =new Circle();
cob.setRadius();

}
}