5

I want to do

public class Settings
{
    static final URL logo = new URL("http://www.example.com/pic.jpg");
    // and other static final stuff...
}

but I get told that I need to handle MalformedURLException. THe specs say that MalformedURLException is

Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.

Now, I know that the URL I give is not malformed, so I'd rather not handle an exception I know cannot occur.

Is there anyway to avoid the unnecessary try-catch-block clogging up my source code?

2 Answers 2

12

Shortest answer is no. But you could create a static utility method to create the URL for you.

 private static URL safeURL(String urlText) {
     try {
         return new URL(urlText);
     } catch (MalformedURLException e) {
         // Ok, so this should not have happened
         throw new IllegalArgumentException("Invalid URL " + urlText, e);  
     }
 }

If you need something like this from several places you should probably put it in a utility class.

Sign up to request clarification or add additional context in comments.

1 Comment

Simple and concise. +1 for // Ok, so this should not have happened
4

Try the following:

public class Settings
{
    static final URL logo;

    static
    {
        try 
        {
            logo = new URL("http://www.example.com/pic.jpg");
        } 
        catch (MalformedURLException e) 
        {
            throw new IllegalStateException("Invalid URL", e);  
        }
    }
    // and other static final stuff...
}

3 Comments

Yes, thanks, I know the syntax and semantics of try-catch, that's not the issue. My problem is that I know that the exception cannot ever occur, which is why I want to avoid clogging my source code with unnecessary try-catch-clauses. In C++ I'd simply disregard the exception and the compiler would not pester me about it, right? In Java... I really think that there should be a way of convincing the compiler to not enforce the exception spec in this case, a compiler directive, if nothing else...
@OppfinnarJocke Oh, I did not intend to offend you—though you just don;t know about static initializers. Unfortunately, there's no way to say Java "just shut up here": if there was such a feature, exceptions would be even more of a mess than they are right now. Point is, every feature interacts with almost every other. Having exceptions magically switched off would mean that the same code could compile or not compile basing on something you don't even see.
Thanks alf. I was not offended, sorry if it came across that way... Yes, I understand that switching off exception checking entirely could mess things up, but a compiler directive to say, as you put it, "shut up here" would seem useful in these rare(?) cases. Anyway, I'll use String instead and create the URL elsewhere in the code (where try-catch seems more appropriate to me). Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.