• 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

A question in exception handling

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
to all Ranchers here.
Why this code is behaving like this.


Here all are fine except with the IOException. is's saying there should be some code which must throw IOException, why it is being like this where Throwable and Exception class are permitted .
[ September 11, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you imported java.io.IOException if yes please do let me know
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does not compile because IOException is a checked Exception. The catches of Throwable and Exception are special cases in that they have to be allowed to enable unchecked Exceptions (subclasses of RuntimeException) to be caught.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get error because IOException is checked Exception. You need to have some code that throws a IOException to catch the same.
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all i got my answer.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we can have empty try for uncheked exceptions but not for checked ones
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys I have also one doubt on this , to my knowledge ClassNotFoundException is also a checked exception , but we can throw it in throws clause of method declaration while not actually throwing it by throw clause from inside method.

Why is such an exception to this exception class ?.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A ClassCastException is an unchecked exception, it extends RuntimeException.

A throws clause indicates only a possibility of an exception being thrown. It does not mean that an exception will always be thrown. If that were the case, most Java APIs would end up throwing exceptions all the time.
[ September 12, 2006: Message edited by: Aniket Patil ]
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aniket you misread that class that is ClassNotFoundexception not ClassCastException , and ClassNotFoundException is a checked exception,and we can't throw a checked exception in method declaration without actually throwing it from codes inside of the method.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i misread the exception you wre talking about.

Do you mean to say that if a method declares that it throws a checked exception in tis signature, and does not throw it from the body, it does not work out.

Take a look at this:


The output is the string above.
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea Aniket i was wrong , thanks for your replies, thanks you all here who have replied.
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic