• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can main() throw exception ?

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34965
377
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic