3

i have one query regarding java standards in case of exception handling.

code snippet:

public String methodXXX(){
//This method may throw IllegalArgumentexception and arrayoutofboundaryException.
}

In this case, Which is good coding standard and please let me know why case1:

public String methodXXX() throw IllegalArgumentexception,ArrayoutofBoundaryException.{
//This method may throw IllegalArgumentexception and arrayoutofboundaryException.
}

case2:

public String methodXXX()throws Exception{
//This method may throw IllegalArgumentexception and arrayoutofboundaryException.
}

Why i am mentioning case2 here is: we might not expect there can be other exception can occur, while run time. As Exception is parent class for all exceptions, case 2 is preferable ??? If yes, in which cases case1 is feasible? Can u please explain me performance point of you also?

pradeep cs
  • 513
  • 4
  • 9
  • 34

4 Answers4

2

It's always much better to declare the specific exceptions a method throws. This not only makes it clearer what possible problems might occur, but it makes it easier to handle them separately if necessary.

There aren't really any performance ramifications: creating the stack trace for an exception is such an expensive operation that it dwarfs all other considerations.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

Both exceptions are RuntimeException subclasses, you don't have to explicitly declare them in the throws part of the method declaration.

In Java there are two kinds of exceptions: checked and unchecked. As explained in the link, each one is used in different circumstances. All exceptions which are subclasses of RuntimeException are unchecked exceptions, and in general you're not supposed to declare that a method throws them.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • It's not really that you're not *supposed* to, it's just that you don't *have* to. It's still useful information to declare them if you explicitly throw them in your code. – Ernest Friedman-Hill Apr 27 '12 at 02:58
1

Typically, RuntimeExceptions like IllegalArgumentException and ArrayIndexOutOfBoundsException reflect programmer error, as opposed to an exception that the final program should know how to handle at runtime.

Generally, it's not worth explicitly declaring RuntimeExceptions, since the proper way to respond to such an exception is usually to just throw up, so the programmer who's running the program knows that there's a bug that needs fixing, and can get the stack trace. It's not usually a good idea to write a try/catch that catches a RuntimeException, so there's no point.

With regards to other kinds of Exceptions, you should declare the specific exceptions that might be thrown, so that users of your method can catch each of them individually and write different responses to each of the specific exception types. Just declaring that you're throwing Exception forces users of your method to write exception handlers that can't do anything intelligent, since they don't know anything about the specific exception that was thrown.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

I think you should not throw exception instead put it in the java doc comment. This way user of your api is aware of what kind of exception can happend and is not forced to capture those exceptions.

/**
 * @throws IllegalArgumentexception
 * @throws ArrayoutofBoundaryException
 */
public String methodXXX() {
  //This method may throw IllegalArgumentexception and arrayoutofboundaryException.
}

I prefer Runtime Exception over checked exception, as most cases forcing to catch exception wont help much. So putting all the exceptions in the java doc make it clear also as well as wont force to catch it. This way the code is cleaner and chances of propagation of exception to top layers is there by default. Else I have seen many developers just catch the checked exception and dont do any thing, leaving top layers un-aware of any exception happening the lower layers.

General thumb of rule is 'If user can recover from exception throw checked exception else document run time exception' And even then I prefer the latter as check exception are generally not very helpful.

havexz
  • 9,550
  • 2
  • 33
  • 29