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();

}
}