How not to return the result boolean in case of exception?

Hello,
I call a routine that returns a boolean to detect the presence of an update. If an exception is triggered, how do you return neither True (an update is available) nor False (update not available)?
I tried “return nil”, with no result.
Thanks.

What is your calling method supposed to do with neither true nor false? If you need a three-valued return, that’s how you should define the method with the exception. Or return a “reason” value in another byref parameter.

I would like to ignore the result if neither true nor false (display nothing).

Hello Denis,
if in the Update() as Boolean function you have caught an exception using a Try Catch block, then you could use the raise keyword to raise again this exception.
Then in the calling method, you can catch again this exception.
In this way, you can manage several exception cases.

This is one those things related to design of a function that, to say what’s one of the best approaches, we need to know what you’re doing and why an exception could occur.

For example, a definition of a needed function to be designed: the function IsDoorOpen should return true/false but if the door mechanism is off, raise the exception 123 : “Can’t read door status”

That’s a perfect good design. And returning a 3rd status makes no sense. YOUR use of it is what should change.

And if you want to capture the status 123 - offline, for example:

Var IsOffline As Boolean = False
Try
  If IsDoorOpen() Then CloseTheDoor()
Catch e
  If e.ErrorNumber = 123 Then 
     IsOffline = True
  Else
    MessageBox("Door error: "+e.Message) // unexpected error of another kind
  End
End
1 Like

Got it. I would like a piece of code to illustrate the interception of the raise in the call method?
Thanks.

Maybe I’m not understanding your question, but the above code intercepted the Exception at the CATCH inside the TRY.

If you need to read more about the topic, here are some directions:

https://documentation.xojo.com/api/code_execution/try.html

https://documentation.xojo.com/getting_started/debugging/exception_handling.html

I would like to ignore the result if neither true nor false (display nothing).

For example, you might do something like this:

Function update() as Boolean

Dim result as boolean

Try
result = getResult()
Catch exc as RuntimeException
//you can intercept it here, and add additional reason
exc.message = "It failed because, who knows."
raise exc
End Try

return result

End Function

then in the caller:

Try
dim res as Boolean = Update()
//it can be false or true, the normal results are here
Catch exc as RuntimeException
//it failed because something happened
End try
1 Like

I don’t get why not just doing it directly instead of 2 unnecessary steps.

Your entire code

Function update() as Boolean

  Dim result as boolean
  
  Try
    result = getResult()
  Catch exc as RuntimeException
    //you can intercept it here, and add additional reason
    exc.message = "It failed because, who knows."
    raise exc
  End Try
    
  return result

End Function


Try
  dim res as Boolean = Update()
  //it can be false or true, the normal results are here
Catch exc as RuntimeException
  //it failed because something happened
End try

Can be directly rewriten just as

Try
  dim res as Boolean = getResult()
  //it can be false or true, the normal results are here
Catch exc // as RuntimeException (without AS TYPE it catches any)
  //it failed because something happened
End try

few options
you can return an array(boolean, Exception or Nil)
you can return an Enum Entry
you can return an String Update,NoUpdate,Error

Yes, of course. But not 2 unnecessary steps, 1.
In that direction, another idea is

if getResult() then
end if
Catch exc

Thank you all. With all these answers, I should be fine.

INTEGER
You could return an Integer. Where:

  • returnValue = 0: your False interpretation (There are no updates)
  • returnValue > 0: your True interpretation (There are updates)
  • returnValue < 0: Connection error

You could even set the returnValue to the number of updated records.

VARIANT
Or you could return a Variant, which could have other values than just True or False.
For instance, you could return

  • NIL, in case there is an exception.
  • False or True for the result of your Function.

In this case you should check the returnValue = Nil. If it’s not, you could check for True or False.