• Post Reply Bookmark Topic Watch Topic
  • New Topic

Throws Exception on the main method  RSS feed

 
Gil Shoam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


in the above code, where is the Exception actually handled? It just keeps getting thrown...
isn't there supposed to be a try/catch somewhere to define what happens if the Exception is thrown?
I know that if I would delete the "throws" from the main, I would be forced to try/catch, but if main throws where is it caught?
Thanks for your time
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gil Shoam wrote:
in the above code, where is the Exception actually handled?


By the JVM

isn't there supposed to be a try/catch somewhere to define what happens if the Exception is thrown?


It's in the JVM, where it calls main().

 
Gil Shoam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer but it didn't really answer my question.
With try/catch I define what happens in case of an exception. (for example i can print "an exception occured"). In this case (main throws) what actually happens? what does the program do?
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gil Shoam wrote:Thanks for your answer but it didn't really answer my question.
With try/catch I define what happens in case of an exception. (for example i can print "an exception occured"). In this case (main throws) what actually happens? what does the program do?

Well, I do not see any try/catch block in your code. Place your try/catch block in the code, try executing the code and repost for further queries.
As answered by Jeff, the jvm is currently doing the exception handling. You can speicfy the behaviour of the application/code in case of exception by using the catch block.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaTM Virtual Machine Specification -- 2.16 Exceptions
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gil Shoam wrote:. . . In this case (main throws) what actually happens? what does the program do?
Bruce Eckel explains it for unchecked Exceptions. If you write... exactly the same happens if that Exception is thrown from the main method.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gil Shoam wrote:Thanks for your answer but it didn't really answer my question.
With try/catch I define what happens in case of an exception. (for example i can print "an exception occured"). In this case (main throws) what actually happens? what does the program do?




Run that and see what happens.

If your next question--or what you really meant to ask--is "How/why does that happen?" then the answer is, as I said, because the JVM's code that calls main is catching the exception and call e.printStackTrace().
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!