Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why main() does not throws Exception by default?

 
Leonardo Shikida
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, it sounds stupid ;-)

But I really think it would be easier this way.

Is there a reason? Like, it's considered bad practice not to catch exception in the main() method?

thanks a bunch
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
who would catch it?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The uncaught exception handler, presumably. Which just does a printStackTrace() and then the thread terminates. Same as happens now for any uncaught RuntimeException or Error.

Leonardo: how would throws Exception be a default? That is, if you wanted to override this default so that main() didn't throw Exception, how would you represent it? Normally we achieve this by not writing "throws Exception". But if that were taken to mean something else, what do we write in its place? "throws no Exception?

Also, what's special about main()? Why not apply this idea to all methods? In which case, why have checked exceptions at all? In fact, there are a number of people who prefer not using checked exceptions, and avoid them where possible. See here for a nice overview of the issues, and some more recent thoughts from the same author here. I think there's something to be said for the idea of getting rid of checked exceptions entirely. But I don't see any reason to give the main() method special treatment here. If we want to remove the problems with checked exceptions, better to remove them from all methods, I think. Or not at all. We don't need a special rule just for main().

Regardless, I think it's very unlikely anything in the Java language will change here. These thoughts may influence future languages. And they've certainly influenced some libraries and frameworks. E.g. Spring uses unchecked exceptions extensively in places most conventional Java programmers would use checked exceptions.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main has unchecked exceptions by default. All these exceptions will be handled by VM and generally will issue its termination. Main has no any differences from any other static public method. It's matter of VM wrapper of calling main(). You can wrtite another VM wrapper which will cal myname() as entry point.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a great explanation Jim.

Thanks for the links
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps another reason main dosnt default to throws Exception, is that it is considered good practise for your application to "exit gracefully" if anything goes wrong, and if main throws the exception, your program has hardly exited gracefully.

However what I do think is "wrong" is that an exception should be an event that is exceptional in the flow of the program, and that in general checked exceptions are over used, and that somethings that are acceptions should perhaps be runtime (or otherwise) errors.

Having worked with C# fairly recently (version2/2005) I find that it has an "unchecked" approach to exceptions, and in my opionon and experience I would rather have the checked exception approach of Java, even if i feel it is often over used.

Sorry just my 2pence and probably way off topic. :S
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formal reason is that, from the language's point of view, there isn't anything special about the main method. It's only the standard JVM that handles it specifically (by using it as the entry point for an application) - the compiler doesn't know anything about this. And other VMs use different entry points - for example for applets.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
The formal reason is that, from the language's point of view, there isn't anything special about the main method. It's only the standard JVM that handles it specifically


The special meaning of main() is not built into the JVM. It is built into the "java[.exe]" program.

You can write a replacement for "java[.exe]" which uses the same JVM, but runs the code using different method(s). Doing so is part of JNI.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic