[JAVA] The story of intentionally using try catch for the first time in my life

Hello, this is Yosuke. qiita1.PNG 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

Previously..

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.

In the first app

qiita1-1.png

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

The story of intentionally using try catch for the first time in my life
Programming for the first time in my life Java 1st Hello World
The story of releasing the Android app to the Play Store for the first time.
[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
The story of learning Java in the first programming
Impressions and doubts about using java for the first time in Android Studio
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
Command to try using Docker for the time being
[Rails] I tried using the button_to method for the first time
Spring Boot for the first time
Spring AOP for the first time
[Introduction] Setting up GridDB Community Edition in the CentOS container of Docker Desktop for the first time
Try using || instead of the ternary operator
Introduction to java for the first time # 2
Try using the Stream API in Java
The story of AppClip support in Anyca
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
Learning for the first time java [Introduction]
The story of writing Java in Emacs
Ideal and reality that I felt when I used Optional for the first time ~ Implementation of cache using Map ~
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
ChatWork4j for using the ChatWork API in Java
Walls hit by Rspec for the first time
Feel the passage of time even in Java
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
Try using the COTOHA API parsing in Java
First try ~ catch
[For beginners] DI ~ The basics of DI and DI in Spring ~
The story of an Illegal State Exception in Jetty.
Learn for the first time java # 3 expressions and operators
Oreore certificate https (2020/12/19) for the first time with nginx
Try running Spring Cloud Config for the time being
Learning memo when learning Java for the first time (personal learning memo)
Try global hooking in Java using the JNativeHook library
The first step to using Xib instead of StoryBoard
The story of making Dr. Oakid using LINE BOT
Try using the query attribute of Ruby on Rails
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
A summary of what Java programmers find when reading Kotlin source for the first time
The story of throwing BLOB data from EXCEL in DBUnit
[Java] Try editing the elements of the Json string using the library
Set the time zone in the JVM of your Azure application
The story of pushing Java to Heroku using the BitBucket pipeline
[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI
Modeling a Digimon with DDD for the first time Part 1
[DL4J] Java deep learning for the first time (handwriting recognition using a fully connected neural network)
Let's try the implementation to find the area of the triangle that we did in the training for newcomers
[Introduction] Installation of Docker Desktop for Mac for the first time and setup of virtual environment construction of CentOS