javaexception

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?


Solution

  • 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");
    }