aspose file tools*
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
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: 374
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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30924
    
158

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: 61646
    
  67

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]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can main() throw exception ?