2

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?

Prasanth G
  • 103
  • 11
  • If you are going to process this exceptions differently then catch it in differents blocks, else you can use `catch (Exception1 | Exception2 | Exception3 e) { }`. – Paul Jul 12 '19 at 08:01
  • 1
    else if(e.getClass().getPackage().startsWith("java.net")) –  Jul 12 '19 at 08:07

6 Answers6

2

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);
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Hey @davidxxx I don't have enough reputations to do a downvote.Btw I have already handled some of the exceptions like malformedurlexception and sockettimeout exception. But for the remaining 10 exceptions, I need to send a message like `"network error occured while trying to access"+url`. This message should not be sent if any IO exception occurs, This should be sent only if an exception belonging to java.net package occurs. So handling all the exceptions by `catch(Exception e)` won't work. I had to write 10 catch blocks for the java.net exceptions but that would be a waste of time. – Prasanth G Jul 12 '19 at 08:36
  • I understand your requirement. But I think that stating at a single place and explicitly exceptions classes that you want to catch is better because you want to apply a specific processing for them. You could use a Set to make it more readable. `Set> exceptionClasses = new HashSet<>(Arrays.asList(ConnectException.class, UnknownHostException.class, ...)); try(..){catch (Exception e) { if (exceptionClasses.contains(e.getClass()){ {send(...)};}` – davidxxx Jul 12 '19 at 11:46
2

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.

Paul
  • 1,410
  • 1
  • 14
  • 30
  • I am not going to process each of the java.net exceptions differently. I am considering all the exceptions that belong to the same package as one. I know your solution could work, but I was looking for a more optimised and simple solution. – Prasanth G Jul 12 '19 at 08:22
  • All right, so unless someone shows me another solution, the comment of **super** up above is the best solution in this case. – Paul Jul 12 '19 at 08:40
2

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; i.e. 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); e.g.

} 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
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • This is the answer I was looking for. Thank you for mentioning IOException even though it doesn't technically cover every single possible network exception. – Anonymous Person May 17 '21 at 20:56
1

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");
}
  • 1
    This solution seems great. but the only problem is `e.getClass().getPackage()` gives the output "package java.net, Java Platform API Specification, version 1.8" (in my machine). So `startsWith` will fail. Please replace the `startsWith` with `contains` :) – Prasanth G Jul 12 '19 at 08:47
0

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?

Rick
  • 935
  • 2
  • 7
  • 22
-1
Example

Try{

Code where your exception occuring

}

Catch(Exception e){
 e.printStackTrace();
}
Sumit Kumawat
  • 127
  • 10