• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Throws Exception on the main method

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JavaTM Virtual Machine Specification -- 2.16 Exceptions
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
reply
    Bookmark Topic Watch Topic
  • New Topic