• 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

how to convert-checked exception to checked exception and to unchecked exception

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is wonderful forum, after login , when I clicked on Java/forms , there is so much information flow between techies about java and certifications…. I felt how I missed till now….

My question is related to exceptions, I read, “Use checked exceptions when the client code can take some
useful recovery action based on information in exception. Use unchecked exception when client code cannot do anything. For
example, convert your SQLException into another checked exception if the client code can recover from it and convert your
SQLException into an unchecked (i.e. RuntimeException) exception, if the client code cannot do anything about it”

Can I have example on how to convert
1. checked exception to another checked exception
2. checked exception to unchecked exception

Thanks in advance…..
-Babu
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

Exceptions can be chained. Basically meaning that one exception is caused by another.
An example



This way you catch the checked exception (IOException) and "convert" it into a RuntimeException. The same can be done for runtime -> checked.

 
babu javagwcc
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Wouter!

Can I have real time example on converting checked exception to un checked exception (with out using custom exception)?

I know, In many realy time projects developers will extend the exception class and they will use those classes to throw custom exceptions.


-Babu
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can replace IOException and CustomRuntimeException with any exceptions you want. It really doesn't matter whether it's a custom exception or a standard Java exception. The code Wouter showed words the same - almost always. The only time it's different is when an exception class doesn't have a constructor that takes another Throwable as a cause. In that case, you can use the initiCause() method instead:

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except initCause returns Throwable. In this case you'd better split that statement:
Or, when the exception has a constructor that takes a cause, use that:
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Except initCause returns Throwable. In this case you'd better split that statement:


Good catch. Or, insert a cast back to the original type, on the same line.

Rob Spoor wrote:Or, when the exception has a constructor that takes a cause


We're already on the what-if-the-exception-doesn't-have-that-constructor branch of the possibility tree.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Must have missed that part. But yes, the cast would make it work as well.
 
Trust God, but always tether your camel... to this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic