Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
babu javagwcc
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:

 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must have missed that part. But yes, the cast would make it work as well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!