Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Can main() throw exception ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can main() throw exception ?" Watch "Can main() throw exception ?" New topic
Author

Can main() throw exception ?

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
Here main() throws exception which is not handled any where. It does not give any error. But shall this not be a compile time error as there is a unhandled exception ?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

nirjari patel wrote: Here main() throws exception which is not handled any where. It does not give any error. But shall this not be a compile time error as there is a unhandled exception ?

main() can throw an exception. It is handled; just not by you. It is handled by the JVM dumping out a stack trace and then ending your program.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Any method can throw an exception that's not handled within the class. In fact, if a checked exception is not handled within a method, it must be declared in the throws clause.

The expectation is that the exception can be handled by higher-level code. In the case of main(), that's the JVM as Jeanne stated.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Consider Paul's rocket mass heater.
 
subject: Can main() throw exception ?
 
Similar Threads
serialization + no valid constructor
Regarding Serialization
serialization
what is the flow of this program?
overridden method throwing exception