Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

try catch

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have question regarding throwing and not throwing exception.

When I write
try{} catch(InterruptedException e){}
// Compiler says exception not thrown in block. also for NoSuchMethodException

What should I look for in code whether those are checked or unchecked exception to discretize for the answer.

Thanks
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an exception is to be caught and if it is a checked exception , then it must be thrown , for it to be caught. For RuntimeExceptions and Errors and their descendants this rule does not hold good.
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So do you mean:

That I should have a code which throws that particular checked exception to be in catch statement.
If I have vaccant try{} block and I am trying to catch any checked exceptions it will always give me compiler error.

Thanks.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is supposed to guard against catch blocks that can never be entered. If the try block cannot throw the indicated exception, then the catch block is unreachable code and should generate a compile-time error.

Specifically, section 14.20 of the Java Language Specification states in part, "A catch block C is reachable iff [if and only if]... some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C..."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365

However, there is a known bug that does allow such code to compile under certain conditions:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4046575
[ April 25, 2005: Message edited by: marc weber ]
reply
    Bookmark Topic Watch Topic
  • New Topic