Question about detecting exceptions in Java

I have an instance method, and inside it I did a simple analysis of web pages:

public void doOperation(final AuthorAccess authorAccess, ArgumentsMap arguments) throws  IllegalArgumentException,AuthorOperationException
{

    final String server = "chiexist1.worldbook.com";
    final String port = "8080";
    try {

             docBuilder = docFactory.newDocumentBuilder();
             doc = docBuilder.parse("http://" + server + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
        ...}
       catch{}
  }

Since I am currently hard-coded the server address in the string, there may be situations where the server name is incorrect, so in this case I want it to automatically change the server URL string to "localhost".

I think the if-else statement will probably work, but I'm not very sure how to define a boolean to determine if this parsing was unsuccessful or not. I also think about putting this in a catch statement, but what about other statements throwing exceptions as well?

API DocumentBuilder, parse() , . , - , URL- , , .

+3
6

, . , catch localhost.

final String server = "chiexist1.worldbook.com";
final String port = "8080";
docBuilder = docFactory.newDocumentBuilder();
doc = null;
try {
    doc = docBuilder.parse("http://" + server + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
} catch{
    try{ 
        doc = docBuilder.parse("http://" + "localhost" + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
    } catch {
        // now we have an error we can't recover from
    }
}
...  // I meant to do this before.
+2
+3

: "catch". , ( ), , , . , , catch - , , .

+1

, ( 5):

String server = "chiexist1.worldbook.com";
final String port = "8080";
int attempts = 0;
final int MAX_ATTEMPTS = 5;
boolean success = false;
docBuilder = docFactory.newDocumentBuilder();
while (!success && attempts < MAX_ATTEMPTS) {
    try {
        doc = docBuilder.parse("http://" + server + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
        success = true;
    } catch (IOException e) {
        if (++attempts < MAX_ATTEMPTS) {
            System.err.println("Attempt to connect to " + server + ":" + port + " failed. Retrying" + (attempts == 1 ? ", but connecting to localhost:" + port + " this time" : "") + ".");
            server = "localhost";
        } else {
            System.err.println("Attempt to connect to " + server + ":" + port + " failed. Giving up after " + attempts + " failed attempts.");
        }
    } 
}
if (!success) {
    // Tidy up and exit
}
// Do stuff
0

You must catch the corresponding exceptions separately and perform different error handling logic depending on which exception you catch. This is considered the best approach because it separates error conditions from normal logic, and u may have different errors, handled differently.

It is considered better than if other statements were made in your normal flow of logic.

0
source

Try something like this:

static String hostname = "hostname";
static{
   if(doesHostExist(hostName)==false){
     hostname="localhost";
   }
}

private static boolean doesHostExist(String host){
  try {
    URL url = new URL("http://" + host);
    URLConnection conn = url.openConnection();
    conn.connect();
    return true;
  } catch (MalformedURLException e) {
    return false;
  } catch (IOException e) {
    return false;
  }

}

* edit the additional code taken from the previous answer: Checking the URL in Java

0
source

Source: https://habr.com/ru/post/1782475/


All Articles