2
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?

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
tareviverat
  • 57
  • 1
  • 8
  • Is this your exact use case? If so then what you want to do isn't necessary and you can just catch the NumberFormatException. – user1675642 Jul 20 '15 at 08:15
  • `if (a>50) throw new CustomException("message")`?? – SatyaTNV Jul 20 '15 at 08:18
  • What is your intend? Smells like a [XY-problem](http://meta.stackexchange.com/a/66378). And maybe you can replace the `CustomException` with an [`IllegalArgumentException`](https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalArgumentException.html). – Turing85 Jul 20 '15 at 08:21
  • Have a look at "exception chaining" before proceeding much further. The answers at the time of my writing this are rubbish. – Bathsheba Jul 20 '15 at 08:31
  • @Turing85 err...yeah maybe...but i just wanted to know whether i can rethrow an exception in the catch block and handle the rethrown exception within the method without using a nested try/catch block. – tareviverat Jul 20 '15 at 14:23

3 Answers3

2

you could refator your example to

 try {
     a = Integer.parseInt(str);
     if (a > 50) {
         throw new CustomException("message");
     }
 } catch (NumberFormatException | CustomException e){
     //do something
 }
user902383
  • 8,420
  • 8
  • 43
  • 63
  • 2
    Your first example doesn't behave the same way as his. You throw a `CustomException` from inside the catch which won't be caught. – Oskar Kjellin Jul 20 '15 at 08:26
  • when i call System.out.println(e.getMessage()); inside the catch block, a NumberFormatException e will give "For input string: ~~" instead of "message". – tareviverat Jul 20 '15 at 14:27
  • @tareviverat is it relevant as in both cases you have same message? – user902383 Jul 20 '15 at 14:58
  • yep, and i'm required to handle with a single method all exceptions thrown, so i need to rethrow a CustomException in the catch block. – tareviverat Jul 20 '15 at 15:22
  • @tareviverat you don't have to rethrow exception, you could wrap everything in function, and handle exception in shared block in place where you call function – user902383 Jul 21 '15 at 07:47
1

Use the Scanner.hasNextInt() to parse the int without worrying about exceptions.

see this question for detailed code.

Community
  • 1
  • 1
Arye Shemesh
  • 582
  • 7
  • 20
0

You could write:

public static void main(String[] args){

    Scanner input = new Scanner(System.in);
    String str = input.next();
    int a;
    try{
        a = Integer.parseInt(str);
        if (a>50) throw new NumberFormatException("message");
    }
    catch(NumberFormatException e){
        //do something
    }
}

but I suggest you to use your version, since the code is more readable. My version, even if removes the inner try, is less readable than yours.

Andrea Iacono
  • 772
  • 7
  • 20