aspose file tools*
The moose likes Beginning Java and the fly likes why main() does not throws Exception by default? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why main() does not throws Exception by default?" Watch "why main() does not throws Exception by default?" New topic
Author

why main() does not throws Exception by default?

Leonardo Shikida
Greenhorn

Joined: Jan 02, 2003
Posts: 29
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

Joined: Oct 02, 2003
Posts: 11320
    
  16

who would catch it?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

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.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thats a great explanation Jim.

Thanks for the links


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why main() does not throws Exception by default?