Hello, this is Yosuke. The third What I am careful about when developing Android apps is the third one I like in more than 300 views. Also attached! Thank you! It will be encouraging. m (v _ v) m
I'm talking about how to try catch if it wasn't intentional, but until now I hit the code as written in the reference book and thought "Oh, it's exception handling" and I'm worried about anything I didn't. So I didn't understand the meaning of try catch. There may have been some dislikes without eating.
When you want to convert binary or hexadecimal numbers with the application you made for the first time, you can not calculate unless the number entered is an integer. If you press the conversion button with a non-integer in the first place, a NumberFormatException error will occur. I found that I wanted to prevent the error somehow with try catch, which I had avoided until now. (Partial excerpt ↓)
MainActivity.java
//If a number other than int type is entered, binary conversion cannot be performed, so error handling
try{
int error = Integer.parseInt(calc_text.getText().toString());
}catch (NumberFormatException e){
calc_text.setTextColor(Color.RED);
Toast.makeText(MainActivity.this,"Conversion can only be done with integers! Or a number outside the range",Toast.LENGTH_SHORT).show();
break;
}
This means that if the entered number is not an Int type, that is, if a NumberFormatException error occurs, the characters will be red and the error content will be displayed as a toast. (Because it is in the switch, it breaks) How convenient! At that time, I was impressed and thought that I was able to grow a little, but after all I can not understand unless I try anything (naturally).
Recommended Posts