• 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

throwing exceptions

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when "main method" throws an exception, where is the exception thrown to and who catches the exception?
example:

sin sai
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe you can catch that exception.
To catch an exception, you need a try-catch block, which must be declared inside a method. The JVM calls main(), not another class, so where could you put try-catch? I guess, technically, the JVM would "catch" the exception by terminating your program and printing a stack trace, but it would do that anyway, so why type the extra code?
The declaration is legal, but I don't think it's particularly useful.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be uncaught, and like Jeff said, the JVM will terminate your application. If you don't want this to happen, then you should catch any exceptions you are expecting within the main() method and handle them appropriately, to avoid your app abnormally ending.
Kevin
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Kevin. I was thinking of having main() inside a try-catch, which would be illegal. I think you're saying that the code could be modified to something like:

Now you're catching the exception, printing a stack trace, but not terminating the program.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sin
Using throws in the main() method may be useful in case you yourself would like to call main() from your program. As in the program. But I guess that this approach is hardly used.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another possibility if you really need to catch Exception from main(): Have only one method call in main(), say, to doMainStuff().

It's not pretty, but I think it would accomplish what Sin Sai seemed to be trying to do.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic