import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = input.next();
int a;
try{
try{
a = Integer.parseInt(str);
}
catch(NumberFormatException nfe){
throw new CustomException("message");
}
if (a>50) throw new CustomException("message");
}
catch(CustomException e){
//do something
}
}
}
If str
is something other than numbers, parseInt
will throw a NumberFormatException
. But I want to 'convert' it so that I'll have a CustomException
with "message" instead. Can I do this without using a nested try/catch
blocks like above?