简体   繁体   中英

Java - Suggestions for catching an exception?

I have an object "LinkFinder" that I'm having throw an exception if it gets a malformed URL, but I don't like the idea of using System.out and just printing an error. Instead of the constructor returning the initialized object, can I force it to return null or something?

private URL url;

//constructor in question
public LinkFinder(String sourcePage) 
{
    try
    {
        this.url = new URL(sourcePage);
    }
    catch (MalformedURLException e)
    {
        System.out.print("Malformed URL Exception: Setting URL to NULL");
        this.url = null;
    }

}

No you can't . The only way your constructor will not return an object (or return null) is if it is not completed.

If you want a null object if the URL is malformed, then the solution would be to apply a try block around your object instantiation code (ie new LinkFinder(url) ), and in its catch block set the reference to null.

Theoretically , this is the sole purpose of exception handling through try-catch block iecatch the expected exception and do the handling as desired(in your case, assign this.url with null ).

If you think that LinkFinder should be gracefully initialized with null in case of invalid sourcePage , your code seems fine. Though you may decide upon System.out vs. logger as per your need.

But Practically , it doesn't make much sense as you are digesting the invalid input and assigning this.url with null which may cause NullPointerException later. It's better to initialize with some default value.

Instead of printing exception on console you can use logger to log exception, and return null as url, But you must check url for null after this,

You can use log4j from apache which is very easy to use.

You do not have to explicitly call this.url = null; , because if an exception is thrown, the url is not initialized, and as it is instance variable, it will have default value set to null . You should use try catch to handle the situation in order to solve this "unexpected behavior".. eg to redirect to a different url .

From your question I assume that the LinkFolder class intention is to handle some kind of data in your application, and it's used by another class that handles the presentation of your program.

The elegant way is that to make the constructor re-throw the exception, so your presentation layer will handle the error using try catch:

public LinkFinder(String sourcePage) throws MalformedURLException
{
    this.url = new URL(sourcePage);
}

The presentation will be like:

try {
    LinkFinder lf = new LinkFinder("whatever");
} catch (MalformedURLException e) {
    System.out.print("Malformed URL Exception.. it has sense to put it right here");
}

Changing the println to the way you want to display your error.

To directly answer the second part of your question, no, you cannot make the constructor return null . The constructor can only return the newly constructed object or throw an error, like @Hernan suggests. The other way to handle it, setting this.url = null requires the external code to check for this condition, or make url private and then only code internal to this class is required to check the condition. You can provide a bool isUrlBroken() method to allow external code to check the condition, if you want.

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