-2

So I'm trying to parse an numeric input in my EditText box. I'm using Integer parse to parse input into int.

@Override
public void afterTextChanged(Editable s) {
    int temp;
    try{
        temp=Integer.parseInt(s.toString());
        //do something

    }catch (NumberFormatException e){
        temp=someOtherNumber;
    }
}

I want to continue app even if user entered invalid number. Is that possible?

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
klo
  • 414
  • 2
  • 6
  • 14
  • Is your application still getting off when `s.toString()` return non numeric value? – SMA Jan 12 '15 at 09:38
  • Yes, i mean, it just exits app ( **** suddenly stop responding)... – klo Jan 12 '15 at 09:40
  • But what should i put in finally? Can i leave it empty to continue? – klo Jan 12 '15 at 09:41
  • Is your app is crashing for this exception?? It must not because you handled that exception. I think there should be the other reason.... – Pankaj Kumar Jan 12 '15 at 09:41
  • possible duplicate of [Java: How to continue reading a file after an exception is thrown](http://stackoverflow.com/questions/8490476/java-how-to-continue-reading-a-file-after-an-exception-is-thrown) – s.d Jan 12 '15 at 09:42
  • You have to catch(Exception e). This will catch any unexpected exceptions have created at runtime. – droidd Jan 12 '15 at 09:42
  • Add the whole watcher code here.. – Pankaj Kumar Jan 12 '15 at 09:43
  • FATAL EXCEPTION main – klo Jan 12 '15 at 09:49
  • java.lang.NumberFormatException: Invalid int: "" – klo Jan 12 '15 at 09:49
  • Cant really copy paste, cause im using AIDE for testing – klo Jan 12 '15 at 09:50
  • Catching the exception and not re-throwing it ensures that the application continues as before. As @PankajKumar mentions, this would imply that you are not showing us the code from where the error is occurring. Either that or the exception being thrown is not a `NumberFormatException`. – Steve Jan 12 '15 at 11:15
  • Yeah. Tnx all for posting, my overview... I did have another parse element in method before. I used parseInt in beforeTextChange() method without try/catch block. I thought that because there is always some number by default in EditText box there is no way an exception can occur in beforeTextChange() method... – klo Jan 16 '15 at 07:49

3 Answers3

0

NumberFormatException you should put when you are confident enough that

1.) Editable s, s is not null. // if not - use Exception class here

If that ius not the case, then catching more prcise exception ie. NumberFormatException is enough. you have already put a catch block, so exception will not be propagated, and you can still continue with your app.

basic Structure

try{

// do your stuff here   1

}catch(NumberFormatException nfe){

// do your stuff here   2

}

// do your stuff here    3

Either do your stuff at 1 and 2 both or just at 3

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • java.lang.NumberFormatException: Invalid int: "" – klo Jan 12 '15 at 09:52
  • Still crushes. I used try/catch block only for parseInt method, put everything else after it, but it still crashes after NumberFormat exception – klo Jan 12 '15 at 10:00
  • @klo are you sure it is crashing in the above code only, i doubt it. code is absolutely fine. – Ankur Singhal Jan 12 '15 at 10:02
0

Yes this is possible. Put only catch able code in try catch block. write your remaining code after above catch block.

Qandil Tariq
  • 539
  • 1
  • 3
  • 15
-1

First check for Editable s it should not null then go further., If you want to catch any exception other than NumberformatException.

then change Catch(NumberFormatException e) to this catch (Exception e). If you have any query please let know.

droidd
  • 1,361
  • 10
  • 15
  • No its NumberFormat exception. I just want to correct error and continue running... – klo Jan 12 '15 at 09:42
  • and rightly so ... Catching Exception is a terrible advice and a bad practice, see http://stackoverflow.com/questions/2416316/why-is-the-catchexception-almost-always-a-bad-idea for example. – 2Dee Jan 12 '15 at 09:47