• 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 and catch

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is necessary to explicitly state in throws clause of a method the types of exceptions that that method may throw but for which their is not a catch handler? Since Java is statically scoped can not the compiler on its own determine if there is a catch handler for a given exception in an enclosing scope by examining which throw statements are present in the method bodies?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right; the Java compiler could generate the throws clauses for a method. The resulting .class files could be identical to those generated using manually-specified throws clauses.
But the result would essentially be that all exceptions would be "unchecked;" you would never know when you needed to catch an exception. You wouldn't know the failure modes of a method you were calling (unless you looked it up, of course, which would make your life harder.) So your code would fail at runtime in surprising ways; you wouldn't know where to add catch clauses until testing pointed them out.
 
babukumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it more like a calling class needs to know what exceptions the super class methods throw or for any other purpose do we need to add the
"throws" clause ?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes, it is appropriate to catch exceptions occured within a method and there are also situations where it is better not to catch the exception and let a method further up the call stack handle it. In this case, the throws clause is the choice for the job.
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have in my application a class which does a lot of the database abstarction. In there I have a method which fethces records from the database and store them in ArrayList. This method declares that is throws SQLException.
The method which actually sends in the the query have then specified try / cacth block so as to determine which query is actually failing.
To contrast this, the method involved on connecting to the database can simple have the try / catch with in it.
Is that a reasonable example? I think I am beginning to understand Exceptions.
 
reply
    Bookmark Topic Watch Topic
  • New Topic