try/catch versus throws Exception

Yes, there’s a huge difference – the latter swallows the exception (showing it, admittedly), whereas the first one will let it propagate. (I’m assuming that showException doesn’t rethrow it.)

So if you call the first method and “do something” fails, then the caller will have to handle the exception. If you call the second method and “do something” fails, then the caller won’t see an exception at all… which is generally a bad thing, unless showException has genuinely handled the exception, fixed whatever was wrong, and generally made sure that calculateArea has achieved its purpose.

You’ll be able to tell this, because you can’t call the first method without either catching Exception yourself or declaring that your method might throw it too.

Leave a Comment