[Gambas-user] Error Management

T Lee Davidson t.lee.davidson at gmail.com
Tue Mar 26 01:20:47 CET 2019


Here is an example use case of Error.Propagate().

I try to, at least loosely, follow the MVC (Model/View/Controller) design principles. The View is my Form and the Controller is 
the Form code. The Model is the business logic that is usually, for me, contained in multiple Classes/Modules that could call 
subroutines in other modules handling such services as data access and web interfaces.

If an error occurs within the Model business logic, I may want to pop up a Message box for user intervention. But, I do not want 
the Model to handle what should be done in the View. I want the View, through the Controller, to handle that.

An error in the Model will not bubble up through the subroutine stack automatically and, if not handled, would cause the 
application to panic at the location of the error. Using Error.Propagate() allows me to pass the error condition up through to 
the Controller so it can present the user with a Message.Error() in the View.

If at one point in the subroutine stack I handle the error, then I should use Error.Clear() to prevent a false positive when 
testing for an error condition further up in the stack.

Here is some code that I hope illustrates some of the concepts:

[code]
' Gambas module file

Public Sub Main()

   OneSub ' Notice I did not: Try OneSub
   If Error Then
     ' Oops. Was the error handled, or not?
     Print Error.Text
   Endif

End

Public Sub OneSub()

   Try TwoSub
   If Error Then
     ' Handle the error.
     ' The Properties of the Error static class should be cleared here if I am actually handling the error condition.
     ' Error.Clear
     Print "There has been an error."
   Endif

End

Public Sub TwoSub()

   Try ThreeSub
   If Error Then Error.Propagate

End

Public Sub ThreeSub()

   Error.Raise("Deeply nested error.")

End
[/code]


___
Lee


On 3/23/19 8:39 AM, Hans Lehmann wrote:
> Hello.
> 
> I looked in the source code of 3.12.2: 0x Error.Clear() and 3x Error.Propagate(). This says a lot about the importance of these 
> two methods. For a chapter on error management in the online Gambas book, I still need a sufficiently good description of what 
> these two methods do. This is especially true for Error.Propagate(). In addition it would be of great advantage for me, if a 
> source code example is attached.
> 
> With kind regards
> 
> Hans
> 
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 


More information about the User mailing list