4

It's a really stupid question but I was wondering something. I have a variable that I have to fill up with an Integer. In some case the source from where I'm getting the data could return an empty string or just be offline. I do not have to handle the offline case because the program will never come this far if the source it's offline. So I thought to do something like this:

    int i = 0;
    try {
        i = mySource.getInt();
    }
    finally {
        System.out.println(i);
    }

int i = 0 is my base value so if I have to parse the empty string I'll just use the initial value. In this case we're gonna generate an exception and it will not be catched. Is there a way to do something like this without handling the exception (so without using catch(Exception e){}) or is it just bad practice?

Just to clarify: I do not need to check if the source is online now because it's a parsed xml file so I'll check if it's offline when I'm downloading the file.

4
  • 2
    I'm really not following your train of thought at all.. Commented Oct 4, 2011 at 19:24
  • 1
    Yeah, not really clear. If you're asking whether it's bad practice to have a try...finally with no catch, no, it isn't. The main problem with checked exceptions is stupid programmers catching them when they have no way to handle them. If you can't recover, don't catch. Commented Oct 4, 2011 at 19:28
  • @erickson: So you have throws Exception on every single method you write in java? The main problem with checked exceptions is that they're checked. Commented Oct 4, 2011 at 21:18
  • @stefan: Of course not, because I want exploit the advantage that checked exceptions offer. But conceptually, what's wrong with having throws Throwable on every single method? Commented Oct 4, 2011 at 22:40

4 Answers 4

10

The exception will propagate if you don't catch it. Since you know what caused the exception, you don't want control flow to be interrupted, and you just want the default value, propagating the exception doesn't do any good. Just catch it and eat it, then use the default value.

The time to use try ... finally with no catch is when you have something that needs cleaning up (a resource that needs closing, usually), but you don't want to handle any exceptions that might get thrown, you want to let them go. This is the opposite case, you don't have anything that needs closing, you just want to squelch the exception and use a default value.

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

4 Comments

+1 if you can/should handle the exception catch it and deal with it
The only way to know whether one can parse a string as an integer is to parse it, so checking beforehand with a regex to catch some cases is pretty useless.
@Voo: it's a little redundant, it avoids creating an exception object, that's about all going for it. it is beside the point, since apparently that api is out of her control, so i'll remove that part of the answer.
@Nathan Hughes Sure it avoids creating an exception object in SOME (not all! and most people miss that and get buggy code) cases, but you basically read the string twice and get the RE overhead, so I doubt it'd be much of a performance win. So it basically just adds some code, but as long as the exception is kept and people understand why it's needed I don't have a problem with checking beforehand.
4

If you control getInt(), make it throw unchecked exceptions and remove the throws clause. Checked exceptions lost the war a few years ago.

5 Comments

Since he doesn't want the exception to propagate, making the exception unchecked would only introduce a possibly hidden bug, which seems like a great reason to go with checked exceptions. In any case he'll have to catch the exception to get the correct behavior.
@Voo: If the exception is never thrown, it doesn't get propagated. If it does get thrown, it needs to either be handled there or propagated. Checked exceptions only pollute this scenario.
@Stefankendall As I understand him, he has a default value he wants to use if the function throws an exception so we have a way to handle the exception. Hence that's a classical example of why a checked exception makes sense (an exceptional situation that may happen without the programmer being able to avoid it and we can handle the situation sensibly).
Or you could use a factory to get the object with initialized defaults, in which case there's still no reason you need a checked exception. getInt probably shouldn't be throwing one anyway.
@StefanKendall In which case the exception would have to be caught in the factory and handled there.. not really different. Now getInt may be the right place to throw an exception or not, depends on the problem, without further information it's hard to say.
2

Checked exceptions are justified when the following conditions are met:

  1. The exceptional condition cannot be prevented by proper use of the API
  2. The programmer using the API can take some useful action once confronted with the exception

Otherwise, unchecked exceptions are more appropriate (as suggested by @Stefan Kendall).

Comments

0

Not sure i follow you, but the finally is used to ensure a block of code is executed when an exception could be raised. i don't see the point of having a finally if no catch is present.

3 Comments

There certainly are times you want a finally, yet no catch. E.g. when you need to clean up something, even when exceptions may occur. You don't necessarily want to catch these exceptions. Unless you expect certain exceptions and can handle them you should never catch exceptions.
good point. i agree with you. I just don't see the point of printing something to system.out in a finally when no exceptions can occur ;-)
Ah, but in this case, exceptions will occur when the content of mySource cannot be converted to an int. The app is then supposed to print the default value 0. So in the OPs case, using finally solves the problem, but her question is still: is this bad practice or not.

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.