You should try to avoid using try / catch, if at all possible, because this is not the preferred method.
The correct way to avoid this error is to stop it before it occurs.
if (mEditText.getText().toString().equals("")) { value = 0f; } else { value = Float.parseFloat(mEditText.getText().toString()); }
Update. Since you are using EditText , the easiest way to avoid a NumberFormatException is to specify the inputType attribute to accept only numbers. XML example: android:inputType="number" . However, you can still get a larger (or smaller) value that the data type cannot store.
Then, depending on your use case, you can even level up using a custom "IntEditText" or custom InputFilter so you can specify a minimum and maximum number and reuse the code in applications.
source share