Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to be able to control if function will throw exception or not

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • 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
 
Amgad Muhammad
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic