Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

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.
 
Bras cause cancer. And tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic