Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

main throws exception ...why

Status
Not open for further replies.

power97

Programmer
Sep 17, 2003
67
0
0
CA
Hi,
I'm relatively new to java. I understand the concept of try/catch blocks. However , looking at some code that I'm supporting I notice that the main block throws some exceptions as follows.

Main Statement:
public static void main(String[] args)
throws ClassNotFoundException, SQLException, Exception)

I'm wondering why this was coded like this ? ( the original programmer is no longer around ). I understand the logic of throwing an exception somewhere in your code where it wouldn't normally throw one but I don't get what having the main throw an exception does. What would handle the main's exception ? I should note that this is run from unix shell scripts.



Thanks In Advance
 
The guy was probably just being lazy, and instead of properly catching possible errors, he decided to 'pass them on' to any higher level program to handle, probably/possibly the user at the command prompt?

Most IDE's have code assist, and/or quick fixes (Eclipse), that can either create an empty try/catch block with the correct exception, or add the exception as a 'throws' parameter.

HTH
TonHu
 
OTOH, it is ofcourse possible that there is another main() method that is merely calling this main() you found so far, and catches the exceptions thrown...

HTH
TonHu
 
It's entirely possible, like TonHu says, that there is some other Java program which executes the main method directly, and so could catch these exceptions where they to be thrown.

It's not what I would call 'good practice', but the main method is just like any other static method when called itself from Java.

Most likely, though, is a lazy programmer.

Tim
 
You *should* catch errors where they belong, not 'expect' some upper-level process to handle that without knowing what to get thrown back at them.
Usually these kind of Main methods are the base entrypoint of an application. If an error occurs, you eithe rcatch them and print som errormessage, or you catch them and ignore/fail without an errormessage, but by convention it still the programmers responsibility to catch & handle any errors. That's why I proclaimed the original programmer to have been lazy. The chance this method is called by another main or method, that catches any errors, still suggests a lazy programmer, as this rather special case should have been documented in the source, so the OP would have never asked his question.

HTH
TonHu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top