0

I usually tend NOT TO catch "Exception", but only the exceptions i expect methods to throw, i hear often that is good practice. But today i came across this issue, an IllegalArgumentException thown by the method URLDecoder.decode(string,encoding). This method is declared as:

public static String decode(String s, String enc)
    throws UnsupportedEncodingException{.....

But it then ('if you look at the source') throws IllegalArgumentException in three different places. So my question to more experienced programmers is, shall i catch "Exception"? or is this method just been declared wrongly? Thank you

JBoy
  • 5,398
  • 13
  • 61
  • 101
  • IllegalArgumentException means simply your method calling is not proper. – Prashant Mar 09 '15 at 13:47
  • Have a look at checked vs unchecked exceptions. Something like http://www.beingjavaguys.com/2013/04/exception-handling-in-java-exception.html. `IllegalArgumentException` is unchecked, `UnsupportedEncodingException` is checked (so is in the method signature). – Holloway Mar 09 '15 at 13:47
  • [You can read the answers of that question.](http://stackoverflow.com/questions/5304098/should-i-put-throws-illegalargumentexception-at-the-function) – code monkey Mar 09 '15 at 13:48
  • post your code. The exception isn't the problem. Your arguments in your methods are. We can help you with that if you post that code. Thanks. – lacraig2 Mar 09 '15 at 13:49

4 Answers4

1

No you should not catch those exceptions. IllegalArgumentException means a precondition has failed. It's usually caused by a bug in your program, and should crash your application. If the input came from the user, detect the wrong input and show a significant message.

If you have an exception handling policy in your application, then you could let this exception bubble out.

Jazzwave06
  • 1,883
  • 1
  • 11
  • 19
0

There are two main types of Exceptions in Java, ones that are declared (eg UnsupportedEncodingException) and Runtime exceptions which you do not have to catch (eg IllegalArgumentException). However, if you do not catch the Runtime exception it will crash your thread and potentially your whole application, so it depends on your use case. When I am building big applications with several threads and doing several things at once, when I call 'unpredictable' methods like 'decode' (especially anything that takes in user input, or reads from files that I am not in charge of, etc) I like to catch Exception, so that I can log a nice clean error or return a meaningful error to the user, but the rest of my application carries on running normally. But if I am writing a single-threaded application where you run it, provide some input, and it gives some output, I would not bother catching Runtime exceptions as the output and stacktrace will be printed to the console and I can see exactly what the problem is - and it doesn't matter that the application died because I can fix the problem and run it again.

Just my personal preference - hope that helps!

Matt
  • 3,303
  • 5
  • 31
  • 53
0

exists two kinds of exceptions in Java, checked and unchecked exceptions, here are the differences:

  • Unchecked exceptions: they have RuntimeException in its inheritance tree. The compiler doesn´t requires you to catch'em explicitly.
  • Checked exceptions: They don´t have RuntimeException in its inheritance tree. You have to catch'em in a try-catch block of declare in a throws clausule.

Basically according to all development methodologies, your code have to be tested, you have to cover all scenarios where unchecked exceptions could occur, because they are generated by errors in your logic code or bad usage of API and you have to correct them. That's why in the final code they should never be catched, because they should never happen.

In the other hand the checked exceptions could happen by causes not related to your code logic i.e. Network loose, Changes in the filesystem priviledges, etc. So you should prepare you code to be ready and catch or delegate the exception.

I hope it helps. And sorry for my english

0

If you take a look at the source of decode(String s, String enc) you will see three places where an IllegalArgumentException is throw:

All three errors are not usage errors but real errors in the input. So it is correct that IllegalArgumentException is not a checked exception.

It is correct that UnsupportedEncodingException is a checked exception. The caller could handle it.

To answer your question: Be as specific as possible in what you catch. Don't just catch Exception because you will have no easy way to decide how to proceed. You could could catch both UnsupportedEncodingException and IllegalArgumentException in different catch blocks. You must catch UnsupportedEncodingException.