Is this a good programming practice?

Putting a "throws Exception" in the constructor.class Testpublic Test [b]throws Exception[/b] {...}
[285 byte] By [kneelmara] at [2007-11-27 9:12:13]
# 1
It's always better to specify the actual exceptions that can be thrown. Leave it out and the compiler will tell you ...
ejpa at 2007-7-12 21:58:29 > top of Java-index,Java Essentials,Java Programming...
# 2
It depends on how you have written your constructor. If you have some statements in your constructor which may throw exception, then it is better to declare throws clause. But if you are just initializing member variables or something else, no need to specify it.
JL.Nayaka at 2007-7-12 21:58:29 > top of Java-index,Java Essentials,Java Programming...
# 3

I read somewhere that the possibility that something can go wrong in a constructor is one of the convincing arguments for having exceptions in the first place, since there's really no other (reasonable) way to report an error back to the caller. So yes, have a constructor throw an exception (and declare which one(s) it may throw) is a very fine practice as far as I'm concerned.

I agree that "throws Exception" is poor practice for all methods. Much better to write "throws Exception1, Exception2, Exception47, ExceptionA, YetADifferentExceptionClass" even if it takes a couple of lines.

Message was edited by:

OleVV

OleVVa at 2007-7-12 21:58:29 > top of Java-index,Java Essentials,Java Programming...
# 4
PS OP, It seems you owe a number of people a sizeable number of Duke stars from earlier threads. Promising stars is not enough, I think, you also have to distribute them.
OleVVa at 2007-7-12 21:58:29 > top of Java-index,Java Essentials,Java Programming...