-1

I created a number format try catch: which is something like this

try{
      int blabla = sc.nextInt();
   } catch (numberFormatException E) {
     doSomething();
   }

How can I go straight to the catch, if say, blabla > 100? Can I go straight to the catch, without having a number format exception, and using if statements? The try catch is mainly for when a string is entered.

user247702
  • 23,641
  • 15
  • 110
  • 157
  • use throw new NumberFormatException() if your blabla>100 inside the try block. – wxyz Feb 25 '14 at 12:21
  • You ... don't seem to understand what catch blocks are for. – Brian Roach Feb 25 '14 at 12:22
  • 1
    @BrianRoach This forum is not a place for you to judge what i know and what I dont. I'm here to learn. If you have any useful advice give it, otherwise, don't comment –  Feb 25 '14 at 12:31

3 Answers3

3

why do you need try catch ? you can simply use if statement

int blabla = sc.nextInt();
if(blabla > 100 )
    doSomeThing();

UPDATED

as per your comment if you want to catch you can throw the exception like following

    try{
          int blabla = sc.nextInt();
          if(blabla > 100)
              throw new NumberFormatException();
       } catch (NumberFormatException E) {
              doSomeThing();
       }

MORE UPDATE

after the Brian comment i realized I should update a bit more. Yes it is definitely wrong to throw an exception for other cases. like here if blabla > 100 then it is not a NumberFormatException. So the better case would be to create your own exception and throw and catch that. to create a custom exception you can see this answer

But i still think the if statement is good for your case.

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Honestly, telling someone how to do something the absolute wrong way is just ... wrong. – Brian Roach Feb 25 '14 at 12:26
  • 1
    @BrianRoach It's not *that* wrong. Why precisely are you so antagonistic about it? Just because it conflates the range error with the number format error? – Marko Topolnik Feb 25 '14 at 12:27
  • @MarkoTopolnik No, because to quote Josh Bloch "Exceptions are for exceptional conditions". They shouldn't be used for flow control. It really *is* that bad of a practice, and I hate seeing beginner programmers get bad advice. – Brian Roach Feb 25 '14 at 12:31
  • @BrianRoach But a number exceeding 100 is as much exceptional as it not being a parseable number. If you extracted all that to a method, which would throw an `InvalidNumberException`, you would call that equally bad? Why, and if not, what exactly is the key difference? – Marko Topolnik Feb 25 '14 at 12:32
  • @MarkoTopolnik conditional != exceptional. Seriously, his book has a whole section on why not to do this :) – Brian Roach Feb 25 '14 at 12:33
  • @BrianRoach Furthermore, just quoting Josh Bloch isn't enough, especially in the exceptions area. His position on checked exceptions is widely violated (Spring/Hibernate), to everyone's relief. – Marko Topolnik Feb 25 '14 at 12:34
  • @BrianRoach I've read the book... a *long* time ago. Even then that item seemed shady to me, now only more so. Note that the example he uses in the book really is extreme, but his generalization extends far too broadly. Exceptions are *all about control flow*. – Marko Topolnik Feb 25 '14 at 12:36
  • @BrianRoach BTW behind any *exception* there's a *condition* in which it is thrown. So your equation doesn't quite add up. – Marko Topolnik Feb 25 '14 at 12:37
  • to be honest I didn't think in depth while answering the question. the first option was my answer. But later from the asker's comment i think he is trying to find a way to throw an exception for custom cases. So here comes the second edit. But again i realaized that for custom cases we should create a custom exception .. – stinepike Feb 25 '14 at 12:39
  • @StinePike Can you name a solid reason *why*? Assuming that handling is exactly the same for all invalid numbers, regardless of the detailed reason (unparseable, out of range). – Marko Topolnik Feb 25 '14 at 12:40
  • please pardon me , i didn't understood your question properly @MarkoTopolnik – stinepike Feb 25 '14 at 12:43
  • Why do you think this calls for a custom exception, which is about 20 lines of boilerplate code? Just to give it a more "politically correct" name? Since the distance between the `throw` and the `catch` is less than a screenful of code, there's little room for confusion. – Marko Topolnik Feb 25 '14 at 12:45
  • i was assuming that he might need to catch a exception for custom case. not only for blabla > 100; just another option.. Also do you think it is good to throw NumberFormatException where it is not actually a caused for invalid number format? And again, the final edit was not only for this question. I mentioned another options for custom need. Please correct my thinking if I am wrong @MarkoTopolnik – stinepike Feb 25 '14 at 12:49
  • `do you think it is good to throw NumberFormatException where it is not actually a caused for invalid number format`---my position on this is the content of my previous comment :) In plainer words: ideally the exception would have a more appropriate name, but it is not worth the extra boilerplate which will be required to declare the custom exception class. – Marko Topolnik Feb 25 '14 at 12:54
  • @MarkoTopolnik yes i agree with you with this point. But i was thinking in a different view. think about a big case instead of simple range checking. I mentioned this only for showing alternative options. but for this case only I think simple if statement is the best case. what do you think? – stinepike Feb 25 '14 at 13:02
  • 1
    If `doSomething` is not just one method call, and it isn't always trivial to extract code into a method (code may depend on many local variables, it may assign to them, there may be multiple exit points...), then your `if` approach results in duplicated code. – Marko Topolnik Feb 25 '14 at 13:31
0

This may be what you are trying to achieve.

try{
  int blabla = sc.nextInt();
  if (blabla > 100) {
    throw new NumberFormatException();
  }
} catch (NumberFormatException E) {
  doSomething();
}
abraabra
  • 464
  • 3
  • 10
0

Try this.

try{
      int blabla = sc.nextInt();
      if(blabla > 100) 
            throw new NumberFormatException();
   } catch (numberFormatException E) {
     doSomething();
   }

EDIT : Stay with if..else until and unless you have specific requirement to go with throw exception and catch it...

Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56