B4A Library lmException

A more useful Exception object.

With this library you can create your own Exception object, continuing to benefit from the one offered by B4A.

Other advantages:

1) lmException has ExtraData, see @Ed Brown' suggestion, post #2.
and HasExtra (same as Intents).
2) B4A Exception message can be only in English, with lmException you can localize this message.


How and why to use it - an example:

If you had this code, using the B4A Exception:
B4X:
Public Sub Something
    Dim ex As Exception : ex = Null

    ex = LoadData
    If ex.IsInitialized Then
        Msgbox(ex.Message, "Loading data")
    Else
        ' do something
    End If
End Sub


Private Sub LoadData As Exception
    Dim ex As Exception : ex = Null

    Try
        Dim Cur As Cursor
        Cur = DB.ExecQuery("SELECT * FROM MyTable")
        If Cur.IsInitialized And Cur.RowCount > 0 Then
            Cur.Position = 0
            Dim Name As String = Cur.GetString("Name")
            If Name = "Luca" Then
                 ' Raise an exception, Luca should be "Mario"
            End if
        End if
        '...
    Catch
        ex = LastException
    End Try

    Return ex
End Sub
With B4A Exception, you can not create an exception as you would need in this case, like "Invalid name".


With lmException, you can write:
B4X:
Public Sub Something
    Dim ex As lmException : ex.Initialize

    ex  = LoadData

    If ex.Raised Then
        Msgbox(ex.Message, "Loading data")
    Else
        ' do something
    End If

End Sub



Private Sub LoadData As lmException
    Dim ex As lmException : ex.Initialize

    Try
        Dim Cur As Cursor
        Cur = DB.ExecQuery("SELECT * FROM MyTable")
        If Cur.IsInitialized And Cur.RowCount > 0 Then
            Cur.Position = 0
            Dim Name As String = Cur.GetString("Name")
            If Name = "Luca" Then
                 ex.Message = "Luca should be Mario"
                 ' or: ex.Message = "Luca dovrebbe essere Mario"
            End if
        End if
        '...
    Catch
        ex.Message = LastException.Message
    End Try

    Return ex
End Sub


As usual, you should copy the two files, lmException.jar and lmException.xml, to your "additional libraries path".
 

Attachments

  • lmException library.zip
    2.6 KB · Views: 196
Last edited:

Ed Brown

Active Member
Licensed User
Longtime User
Hello @LucaMs

Nice and simple. As lmException is being returned it would be easily adapted to handle additional flags/statuses. ie. in your example calling LoadData returns lmException. lmException could be expanded to provide not only exceptions but success statuses to indicate the success of the load. eg. how many records were successfully loaded.

These are just my initial thoughts about possible usage and not to be taken as a 'best practice'.
 

LucaMs

Expert
Licensed User
Longtime User
Hello @LucaMs

Nice and simple. As lmException is being returned it would be easily adapted to handle additional flags/statuses. ie. in your example calling LoadData returns lmException. lmException could be expanded to provide not only exceptions but success statuses to indicate the success of the load. eg. how many records were successfully loaded.

These are just my initial thoughts about possible usage and not to be taken as a 'best practice'.


Suggestions are always welcome, thank you.

This class will be for everyone, so anyone with ideas to improve it...!
 

Ed Brown

Active Member
Licensed User
Longtime User
Yep, just like that ;)

I haven't tried anything like this yes but, I was just thinking that using 'Object' instead of 'Map' might provide some flexibility so that a 'List' or 'Array' or 'whatever' (I know that's not a type) could be used. Just not sure if 'Object' can take a collection type??
 

LucaMs

Expert
Licensed User
Longtime User
Thinking better...

If I change ExtraData type to Object, then you need to use GetType to know the type.

If you need to add arrays, lists, ... you can simply:

Dim m As Map
m.Initialize
m.Put("Persons", lstPersons) ' <--- lstPersons is of type List, obviously
ex.ExtraData = m
 
Last edited:

Ed Brown

Active Member
Licensed User
Longtime User
Nice.

It's a good way of being able to add different values/collections by name as well. Good thinking!
 
Top