Try, Catch Question

I’ve recently been re-writing some my code to use custom Exceptions. However I’m beginning to feel that this was a design mistake.

Try ~ Do Something Catch err as myException #if appMode = editor then // Write error to log, so all errors are shown at the end. #else // Report error and exit method return #endif End Try

The problem I’m running into is that this method contains several of these blocks and when the app is in editor mode, I want to continue processing the rest of the code in this function, but when it’s in client mode I want it to abandon there and then.

I see from the language reference that I can use Finally but then that requires nested Try/Catch blocks and to be honest I try to avoid nested anything as much as I can. Thoughts?

Nested:

// --- Try first thing.
Try
    ~ Do Something
Catch err as myException
    #if appMode = editor then
        // Write error to log, so all errors are shown at the end.
    #else
       // Report error and exit method
       return
    #endif

Finally
     // --- Now try second thing.
    Try
        ~ Do Something
    Catch err as myException
        #if appMode = editor then
            // Write error to log, so all errors are shown at the end.
        #else
           // Report error and exit method
           return
        #endif

    Finally
        // --- Now try third thing.
        Try
            ~ Do Something
        Catch err as myException
            #if appMode = editor then
                // Write error to log, so all errors are shown at the end.
            #else
               // Report error and exit method
               return
            #endif
        End Try
    End Try
End Try

Unnested code

[code]Try
~ Do Something
Catch err as myException
#if appMode = editor then
// Write error to log, so all errors are shown at the end.
#else
// Report error and exit method
return
#endif
End Try

// — Now try second thing
Try
~ Do Something
Catch err as myException
#if appMode = editor then
// Write error to log, so all errors are shown at the end.
#else
// Report error and exit method
return
#endif
End Try

// — Now try third thing.
Try
~ Do Something
Catch err as myException
#if appMode = editor then
// Write error to log, so all errors are shown at the end.
#else
// Report error and exit method
return
#endif
End Try
[/code]

Untested idea…

Since you are raising your own exceptions, why not let it raise itself if appropriate? One way might be:

// some code
dim err as new MyException

// next bit of code

Or perhaps:

MyException.MaybeRaise // shared method

In either case, the exception will decide whether to write to a log or raise itself and you won’t need the Try/Catch blocks at all, at least not for this purpose.

I think I have a fundamental design issue here, and that’s in choosing how to handle errors. Especially in this function as I want error handling to be different dependent on the nature of the app that’s using this module.

99& of time it will operate in client mode, where it’s fine to bail the moment it hits any kind of error. However when the module is used in the editor for that 1% of the time, I want it to report back to the user all of the errors that it encounters at once.

Some other design patterns that I’ve seen with Apple APIs are as follows.

Dim errCode as integer = myfunction( inValues, byRef outValue )
Dim rValue as [whatever] = myFunction( inValues, byRef error as NSError)
Dim completed as boolean = myFunction( inValues, byRef outValue, byRef error as NSError)

@Sam Rowlands I dont have a good answer for you. I dont use enough try/catch blocks to be helpful. I need to use more myself. I am too new to using custom exceptions. I have been using the Xojo and 3rd party ones for a while, just not my own. I do have to say that every day I use xojo, I get just that much better as an OOP programmer (came from the non-OOP/functional world).

If you work out what the best way to do this, please let us know. it would be a good learning piece for many of us.

In the end I went with the following model.

Dim completed as boolean = myFunction( inValues, byRef outValue, byRef error as NSError)

// ---- If the HTML needs saving then. if saveHTMLTemplate and not writeSignedDataToDisk( f.child( "HTMLTemplate.htm" ), myData.HTMLTemplate, error ) then _ errorLog.append "?? Writing HTMLTemplate error: " + error

Felt it gave me a bit more control and allowed a method to display a list of errors at once (at the end of the method).