• 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 be able to control if function will throw exception or not

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
i have function in my application that it use to catch exception inside it and only return true or false based on the exceptions.
this function is api function that external applications are using .
now there is demand that this function will throw exceptions , but how can i throw exceptions and also keep the backward capabilities. to the old users.
i can add another parameter to the function and then cause it to throw exceptions ? how can it be done technically ?
what will be with the old users i don't what them to handle the exceptions
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1-you could make another method that does the same thing as the old method

2-make it throw the exceptions you want

3-make the old method does nothing more calling the new one

4-handle the exception throwen from the new method in the old metod so by that the old method will not have to throw any new exceptions
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to maintain backwards compatibility then any exceptions you add must be runtime exceptions i.e. extend RuntimeException class.
The only way to throw a checked exception is to create a RuntimeException with the checked exception as the cause and then throw the RuntimeException.
 
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

Originally posted by Joanne Neal:
If you need to maintain backwards compatibility then any exceptions you add must be runtime exceptions i.e. extend RuntimeException class.


That would still break old code - programmer's are checking against a return value of false, not a RuntimeException.

I'd go for Amgad's advice.

Old situation:

New situation:

I think the trickiest thing is finding a name for the new method that still looks enough like the old one
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

That would still break old code - programmer's are checking against a return value of false, not a RuntimeException.



Good point
reply
    Bookmark Topic Watch Topic
  • New Topic