• 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

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void m(){
try{}
catch(Exception e){}
}
public void m1(){
try{}
catch(IOException e){}
}
Why does the method m1() complain but m() does not since it too does not throw any exception in try ? Why is it allowed also to throw exceptions in the method declaration without the method actually throwing any (blank method for eg)? any help will be appreciated.
Thanx
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you have forgotten to import java.io.IOException when compiling?
 
Soum Sark
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No in fact the compiler message is
java.io.IOException is never thrown in body of corresponding try statement
catch(IOException e){}
^
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not allowed to catch checked exceptions (such as IOException) that can never be thrown.
You are allowed to catch unchecked exceptions anywhere. In particular, you can catch Exception itself because some of its subclasses are unchecked (RuntimeException and its subclasses).
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Newman:
You are not allowed to catch checked exceptions (such as IOException) that can never be thrown.
You are allowed to catch unchecked exceptions anywhere. In particular, you can catch Exception itself because some of its subclasses are unchecked (RuntimeException and its subclasses).


Hi everybody,
Could somebody please point to the part(s) of JLS where the aforementioned paragraph could be deduced from?
Thanks a lot
 
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic