How do I handle errors that are already handled by another error?

user9549355

I am connecting to a website using Jsoup. I have inserted a .timeout to check if the website times out.

Because I already handle IOException, my IDE is not allowing me to put another catch for SocketTimeOutException, which is fair, I guess.

Is there a way to tell from IOException what is the error? I'd like to have specific debugging output for different errors, so that I can keep the code clean and efficient.

Here is my code (yes, some variables are missing, they are pretty simple though and not really necessary):

Document doc = null;

try
    {
        doc = Jsoup.connect(Url).proxy(proxy).userAgent(userAgent).timeout(10000).get();
    }
catch (IOException e)
    {
        if (...)
            {
                if(specificThread.running == false) {
                    System.out.println("Thread got interrupted! Link visited -> "+ Url);
                    Thread.currentThread().interrupt();
                }

                try
                    {
                        System.out.println("Localhost detected! If this happens, you're stupid");
                        doc = Jsoup.connect(Url).userAgent(userAgent).get();
                    }
                catch (IOException e1)
                    {
                        System.out.println("Couldn't connect to " + Url + " , localhost was detected.");
                        e1.printStackTrace();
                        System.exit(-1);
                    }
                catch (java.lang.IllegalArgumentException error)
                    {
                        System.out.println("Malformed URL detected ->  " + Url) ;
                    }
            }
        else
            {
                System.out.println("Couldn't connect to " + Url);
                e.printStackTrace();
            }
    }
catch (java.lang.IllegalArgumentException error)
    {
        System.out.println("Malformed URL detected ->  " + Url);
    }
catch (java.net.SocketTimeoutException error) //IDE Is blocking this
    {
        //Handle error here
    }

Is it possible to do this?

Andy Turner

Put the catch for the SocketTimeoutException before the catch for the IOException.

Exception handling works by looking through a table until a matching exception type is found. As such, more specific exceptions have to come before more general exceptions, in order that they can be matched.


This is described in JLS Sec 14.21, "Unreachable Statements":

It is a compile-time error if a statement cannot be executed because it is unreachable.

...

  • A catch block C is reachable iff both of the following are true:

    • ...
    • There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

So, you just need to make sure there is no earlier catch block catching a superclass parameter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get errors which are handled serverside in a ajax error message after submitting a form?

How do I handle Import Errors?

How do I handle errors in a deferred function?

How do I handle errors with promises?

How do I handle JavaScript Fetch errors?

How do I handle RapidXml errors?

How do I handle transaction errors?

How Do I Handle Errors Globally in TestCafe

How do I handle stripe errors for "redirectToCheckout"?

How do i handle errors in Dio package

How do you handle errors when calling another class initializer?

How do I handle "cancel" intent when already in a dialog?

How do I handle data that already has headers?

How do I handle errors in passport.deserializeUser()?

How do I handle errors in a worker pool using WaitGroup?

How do I handle errors in mapped functions in AWS Glue?

How do I handle errors when responseType is blob using Vuejs?

In React, how do I handle errors coming back from the server?

How do i handle errors correctly and prevent showing folder directory

How do I handle errors in an f-string?

How do I handle different errors when working with an API?

How do I handle errors from cleanup / destroy functions

How do I return the success value of chaining multiple Results if no errors occur or another value if any error happens?

How do i handle error properly in Laravel?

How do i handle this dividing modulus error?

How do I handle this Git error?

UnhandledPromiseRejectionWarning: Error: Request is already handled

How do I make a class (that already exist) inherit another class?

How do I open window from another that's already open?