B4J Code Snippet [B4X]Raise Your Own Exceptions

Currently writing a cross platform library and I want to throw my own exceptions for cases like division by zero etc.

B4X:
Sub Class_Globals
    
    #If B4i
       Private Exception As NativeObject
    #Else
       Private Exception As JavaObject
   #End If
   Private ExceptionName As String
   Private ExceptionReason As String
   Private ExceptionModule As String
   Private ExceptionSub As String
   Private ClassModule As Boolean
   Private ExceptionDescription As String
   
    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Name As String,Reason As String,Module As String,Method As String)
    Dim SB As StringBuilder
    ExceptionName = Name
    ExceptionReason = Reason
    ExceptionModule = Module
    ExceptionSub = Method
    SB.Initialize
 
    SB.Append("Module: ").Append(ExceptionModule).Append(", ") _
 .Append("Sub: ").Append(ExceptionSub).Append(", ") _
 .Append("Reason: ").Append(ExceptionReason)
 
 ExceptionDescription = SB.ToString
    #If B4i
       Exception.Initialize("NSException")
   #Else
      Exception = Me
    #End If
        
End Sub


Public Sub Raise

 #If B4J OR B4A
    Exception.RunMethod("raiseException",Array(ExceptionDescription))
  #Else
     Exception.RunMethod("raise:format:",Array(ExceptionName,ExceptionDescription))
  #End If
    
    
End Sub

#If Java
  import java.lang.RuntimeException;
  
  public void raiseException(String description)  {
      java.lang.RuntimeException e = new RuntimeException(description);
      throw e;
     }
#End IF

Usage:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Dim Exception As B4XException
    Exception.Initialize("Test","Test Exception","Main","AppStart")
    Exception.Raise
End Sub

Output:

B4A/J
B4X:
Caused by: java.lang.RuntimeException: Module: Main, Sub: AppStart, Reason: Test Exception

B4i

B4X:
Error occurred on line: 47 (B4XException)
Module: Main, Sub: Application_Start, Reason: Test Exception
 

Diceman

Active Member
Licensed User
How about creating a Type for the information you are collecting?

B4X:
    Type TDtaException(ErrorNum as Int, ExceptionName As String, ExceptionReason As String, ExceptionModule As String, ExceptionSub As String, ClassModule As Boolean, ExceptionDescription As String)
    Dim MyDtaException As TDtaExceptionData

That way you can pass MyDtaException as a single parameter to a sub or return it in a function. It can be easily updated with new variables and is easier to maintain compared to global variables. I would also add an ErrorNum variable so your (multi-language?) program can handle it easier than relying on ExceptionName.
 
Last edited:
Top