• 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

exceptions: declare or handle

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The golden rule is that any exceptions either declare them or handle them. The following is declaring and handling same exception. I thought that will cause compiler error.
But it did NOT.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java allows you to declare that you throw an exception even if you in fact don't throw it (as in this case).
If your main() called m(), then main would have to either handle Exception or declare that it throws Exception.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that if you declare a checked exception, the compiler may complain if it can tell by inspection that there's no way that checked exception could have been thrown by the method. But unchecked exceptions could in theory be thrown by just about any code, so the compiler doesn't complain if you declare them. Declaring "throws Exception" (or "throws Throwable") is a special case; an Exception (or Throwable) could be either checked or unchecked, so the compiler assumes that since it could represent an unchecked exception, it's not going to complain if you declare the Exception, even if the compiler can't see how the exception might be thrown.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the compiler ever complains under those circumstances. It accepts this without complaint:
class ExTest {
void foo() throws java.sql.SQLException {}
}
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Ron. The compiler will not complain if method is only declaring that it might throw such and such exception and code is evidently not throwing anything. However, if there is a try region with code that is evidently not throwing anything, compiler will complain. Jim maybe that is what you were trying to express in your post.
Thanks
Barkat
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. The compiler will complain about this:

The error is:
ExTest.java:5: exception java.sql.SQLException is never thrown in body of corresponding try statement
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right - the try/catch is the case I was thinking of. Sorry, nevermind...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic