aspose file tools*
The moose likes Java in General and the fly likes how to be able to control if function will throw exception or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to be able to control if function will throw exception or not " Watch "how to be able to control if function will throw exception or not " New topic
Author

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

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Mar 26, 2008
Posts: 46
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


SCJP5-(100%)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
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.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to be able to control if function will throw exception or not