简体   繁体   中英

How to handle all the java.net exceptions in a single catch block?

I am trying to handle the exceptions that are a part of the java.net package. I went through the documentation and I saw 12 exceptions belonging to that package, but I couldn't find the parent class of these exceptions.

So far what I have tried is:

catch(Exception e)
{
    if(e instanceof org.openqa.selenium.WebDriverException)
        sendException("Problem is with the selenium web driver");
    else if(e instanceof java.net.Exception) //I need help with this line
        sendException("There is some problem with the network");
}

After trying this, I get the following error message

unable to resolve class java.net.Exception

How can I catch the java.net exceptions?

java.net.Exception doesn't exist, java.lang.Exception does.

To check that the package is java.net, use e.getClass().getPackage():

catch(Exception e)
{
    if(e instanceof org.openqa.selenium.WebDriverException)
        sendException("Problem is with the selenium web driver");
    else if(e.getClass().getPackage().startsWith("java.net"))
        sendException("There is some problem with the network");
}

java.net.Exception doesn't exist and you cannot catch exceptions of classes from a specific package in this way.
Considering that network exceptions always start by java.net is wrong too.
Some network exception don't start by java.net and exceptions can also be wrapped by another exception. For example java.net.SocketException could be wrapped by java.lang.IllegalStateException .
You will not handle it because that is not a java.net exception.

Handling the exception with instanceOf looks not helpful either.
Specifying a specific exception in the catch clause is enough.
Note also that an exception is self-explanatory and should contain in its state all relevant information : you don't need to map the exception to a textual message as you do.

What you want to send/log is exploiting the exception class/message/cause/stracktrace associated to. What you do is just interpretation of the exception meaning that would be error prone.

so do that :

catch(Exception e){
    sendException(e); 
}

public void sendException(Exception e){
   // exploit e.getClass(), e.getMessage() or e.getStackTrace()
   ...
}

Note that in some cases you want to catch some specific exceptions because you have a specific processing for them :

catch(WebDriverException e){
     processWebDriverException(e);
}
catch(Exception e){
     processAnyOtherException(e);
}

You can catch java.net Exceptions like this :

try {

}
catch (
    BindException 
    | ConnectException 
    | HttpRetryException
    | MalformedURLException
    | NoRouteToHostException
    | PortUnreachableException
    | ProtocolException
    | SocketException
    | SocketTimeoutException
    | UnknownHostException
    | UnknownServiceException
    | URISyntaxException
    e
){
    // do stuff with e
}

But what is the point if you are going to call instanceof after to process each exception differently?

In this case you should use different catch blocks.

First off, the reason that you can't catch java.net.Exception . The Exception class is in the java.lang and it is NOT the exception you should be catching.

So what should you catch?

Unfortunately, there there is no suitable supertype for all networking exceptions and no non-networking others. But it turns out that most networking exceptions have java.io.IOException as an ancestor.

(There is one java.net.* exception that doesn't descend from IOException ; ie java.net.URISyntaxException . But that is an unchecked exception, and it is not indicative of a "network problem".)

So one approach would be to catch IOException . You could further refine this by using reflection to get the package name of the exception class (see https://stackoverflow.com/a/57002571/139985 ); eg

} catch (org.openqa.selenium.WebDriverException e) {
    sendException("Problem is with the selenium web driver");
} catch (IOException e) {
    if (e.getClass().getPackage().startsWith("java.net"))
        sendException("There is some problem with the network");
    else {
        throw e;  // Or diagnose differently
    }
}
Example

Try{

Code where your exception occuring

}

Catch(Exception e){
 e.printStackTrace();
}

If there is no common superclass, there is no way to catch these exceptions in a single clause. You might want to check out this post, but I would agree with you that it's not pretty if you have to specify 12 exceptions:

Can I catch multiple Java exceptions in the same catch clause?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM