[JAVA] I found no way to get the error code when I received an exception on Android


With Swift or ObjC The error code is included in the caught NSError

let e: NSError = NSError(domain: "unko", code: 931, userInfo: nil)
print(e.code) // <- 931

On the other hand, when I received an exception with HttpURLConnection on Android Java, such a message was displayed. ``failed to connect to /...** (port *****): connect failed: ECONNREFUSED (Connection refused)``` I wondered if this ECONNREFUSED had an error code in Excption, but I searched for it, but getCode like Swift()There is no such thing Eh, getLocalizedMessage()Is it classified from the character string? It is a such a fool I was in trouble with that feeling, but I was a little addicted because no information came out even if I searched #I found it

URL url = new URL("http://unko.com");
HttpURLConnection connection = null;
try {
    connection = (HttpURLConnection) url.openConnection();
 connection.connect (); // Exception coming
    Log.d("unko", "" + connection.getResponseCode());
    Log.d("unko", "" + connection.getContentType());
} catch (Exception e) {
    Log.w("unko", "" + ((ErrnoException) e.getCause()).errno);
} finally {
    if (connection != null) {

I got the code in this part

((ErrnoException) e.getCause()).errno // == 111

This 111 seems to come from Linux System Errors There is no doubt that I have also sent a message such as ETIMEDOUT ...

Errors: Linux System Errors http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html

By the way, they had constants defined OsConstants.ECONNREFUSED

So I got the error code


#Summary -It's annoying to just take the code -I haven't investigated it properly, so if there is another method, it will be serious. -Java is dung

Recommended Posts

I found no way to get the error code when I received an exception on Android
I have a question. I get an error when playing a video in Listview on android.
I tried to summarize the stumbling points when developing an Android application
I want to display an error message when registering in the database
I get an error when adding a dependency
When I renew the certificate with CircleCI × fastlane, I get an exit status: 65 error.
I get an error when I try to use "^" or "$" in ruby ​​regular expression
Beginner: I get an error on line 30. I don't know where to fix it.
When I try to run docker-compose, I get the error "Cannot locate specified Dockerfile: Dockerfile"
I want to simplify the log output on Android
Throw an exception and catch when there is no handler corresponding to the path in spring
I want to make the frame of the text box red when there is an input error
If you get a "Quantum Renderer: no suitable pipeline found" error when running the jar file
I get a Ruby version error when I try to start Rails.
I tried to translate the error message when executing Eclipse (Java)
[Android] I want to get the listener from the button in ListView
[Android] Get the date on Monday
Get JUnit code coverage on Android.
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
[Android Studio] What to try when you get into error spitting hell
[Visual Studio Code] I get a syntax error when debugging when using rbenv
[Docker] How to build when the source code is bind-mounted on the container
no space left on device What to do if an error occurs
I want to get the IP address when connecting to Wi-Fi in Java
What to do if you get an error when you hit Heroku logs
[Rails] What to do if you get an error saying "Could not find a JavaScript runtime." When executing the rails s command on Catalina
When I tried to start GlassFish, I got an internal error while "Publishing to GlassFish 4.0 on localhost ...". What to do when it becomes
What to do if you get an "A server is already running." Error when you try to start the rails server
In WSL2, when I did `docker-compose up`, I got an error saying that the sh file was not found.
What I tried when I wanted to get all the fields of a bean
Points I stumbled upon when creating an Android application [Updated from time to time]
[Heroku] How to solve when an error is displayed on git push heroku master
What to do if you get an error on heroku rake db: migrate
I managed to get a blank when I brought the contents of Beans to the textarea
[Android] Get the tapped position (coordinates) on the screen
Migration from Eclipse to IntelliJ (on the way)
When deploying to Heroku, OpenApp causes an error
The code I used to connect Rails 3 to PostgreSQL 10
I got an error when I ran rake routes.
I want to get the value in Ruby
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
The story that did not disappear when I tried to delete mysql on ubuntu
What to do if you get the error Couldn't find Item without an ID
What to do if you get a port error when docker-compose up on Mac
I got an error when trying to install sbt to build a Scala development environment
Customize the display when an error such as 404 Not Found occurs in Spring Boot