-1

I am writing a calculator program in Java using Android Studio.

I have a try/catch block in my program, with the catch block catching a NumberFormatException for invalid input.

} catch (NumberFormatException e) {

}

Without this try/catch block and I don't handle the exception of a user entering invalid input, a message pops up saying "Unfortunately, CalculatorApp has stopped" and the app closes.

With this exception, upon entering invalid input the app doesn't close (good!) but nothing happens when the user clicks he enter button.

How do I make it so a message pops up upon catching the exception?

3 Answers3

2

Just put a toast message inside the catch block, like this ...

Toast toast1 = Toast.makeText(getApplicationContext(), "Invalid input",Toast.LENGTH_SHORT);

toast1.show();
jonhid
  • 2,075
  • 1
  • 11
  • 18
2
} catch (NumberFormatException e) {
 Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();

}

Use this it will show message to user. or if yo want to show the exception to user then use

} catch (NumberFormatException e) {
     Toast.makeText(this, "Invalid Input" +e, Toast.LENGTH_SHORT).show();

    }
Rishabh Mahatha
  • 1,251
  • 9
  • 19
1

In the catch block , you can display a Toast message When the exception occurs.

Hasif Seyd
  • 1,686
  • 12
  • 19