Click to See Complete Forum and Search --> : Defying the return type


aatwo
04-04-2007, 01:56 PM
Is there a way for a method to not return anything if a certain condition is met, despite the fact that I have specified an integer return type for that particular method?

AlaNio
04-04-2007, 02:36 PM
return null ?

aatwo
04-04-2007, 02:58 PM
null is not of type int and so throws up an error

AlaNio
04-04-2007, 03:01 PM
it shouldn't do. are you mapping the result to a variable of type int ?

aatwo
04-04-2007, 03:11 PM
nope I'm not mapping it at all.

Don't worry about it but I have decided to just throw an exception.

agent_x91
04-17-2007, 05:43 AM
No, there's no way of "defying the return type" as you put it. If the return type is a certain type, you must return a variable of that type. If you want to return an int, you have two options:

Either return -1 as the int, and handle a -1 as if nothing was returned, or make it return an Integer instead of an int, and you can return null legally.