Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

should our code throw InternalError()?

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am having a design issue. I have a clone method impl for my object and I was wondering if I should do something like,



Personally, I feel I should not throw InternalError() as I don't want caller to fail w/o allowing some way to log or something...If I throw InternalError then client will die right away...

But if super.clone() fails there is some serious error, so in that sense it might be okay to throw InternalError...

I am not clear about how should I go. Can anybody help me?

Also, I have noticed this sort of behavior in java.util.ArrayList's clone() method ...

Regards,
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just one clarification- I am not designing API or something. MyObject and its client code both are written by me as part of my application so its not really a "big" issue I guess but I was just wondering what is good way...

Regards
Maulin
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think you should never throw InternalError. The JDK documentation says that it is "thrown to indicate some unexpected internal error has occurred in the Java Virtual Machine". That is not a situation that you detect in your own code, so you should never throw it.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have full control over this then you'd be testing for a CloneNotSupportedException that should never happen. In that case it sounds like a great opportunity for an assertion, e.g.:

Does that fit?

Jules
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good suggestion. Still you do have to catch that moronic CloneNotSupportedException (surely that should have been unchecked!) and do something with it; you certainly don't want to throw it away. My suggestion would be to wrap it in an application-specific RuntimeException subclass or even an Error subclass because something must be horribly wrong for this to happen.

Logging shouldn't be an issue, assuming you have some infrastructure at the top of the call stack that logs any unchecked exceptions that propagate all the way up.

- Peter
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all,

Thanks so much for all your reviews.

In essence it seems that,
1. InternalError shouldn't be thrown
2. Some custom exception (or just CNSE) should be thrown in case we get CNSE in super.clone() call

I think I am clear now.

Thanks
Maulin
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic