• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why main() does not throws Exception by default?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
who would catch it?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a great explanation Jim.

Thanks for the links
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic