B4J Question How to catch e re-throw "Port not found" exception?

mauro vicamini

Active Member
Licensed User
Hello Evryone!
I use jSerial library to connect to some device on serial/usb port.
I've a class that manage the connection with the devices and I'd like to catch the "Port not found" exception if the specified port doesn't exist.
I've used this code in the class that manage the connections:
B4X:
Sub Class_Globals
    Dim objSerial As Serial
    Dim mPort As String
    Dim mEventName As String
    Dim objCaller As Object
    ...
  
End Sub
Public Sub Initialize(Port As String,EventName As String,Caller As Object)
    objSerial.Initialize("objSerial")
    mPort = Port
    mEventName = EventName
    objCaller = Caller
End Sub
public Sub Connect()
    Dim exc As ExceptionEx
    Try
        objSerial.Open(mPort)
        objSerial.SetParams(9600,8,1,0)
        objAsyncStream.Initialize(objSerial.GetInputStream,objSerial.GetOutputStream,"objAsyncStream")
        If SubExists(objCaller,mEventName & "_Connected") Then
            CallSubDelayed(objCaller,mEventName & "_Connected")
        End If
    Catch
        exc = LastException
        exc.Throw
    End Try

End Sub
and in the main module I use the this code to connect and catch the exception:

B4X:
   Sub AppStart (Args() As String)
    Try
        objConn.Initialize("COM11","objConn",Me) ' COM11 doesn't exist
        objConn.Connect()
        StartMessageLoop
    Catch
        Log("Error " & LastException.Message)
    End Try
End Sub
But the execution in case of not found port ends on "exc.Throw" line on the connection class with the exception "jssc.SerialPortException: Port name - COM11; Method name - openPort(); Exception type - Port not found." without propagate the exception to the main module.
What I'm missing or doing wrong?

Thanks for the attention.
 
Last edited:

mauro vicamini

Active Member
Licensed User
I attach another question: I've compiled my class as library but when I use it in a test program with the same code of the previous AppStart with a port that exist I got this exception:
B4X:
java.lang.RuntimeException: Field: _objConn not found in: b4j.example.main
    at anywheresoftware.b4a.shell.Shell$FieldCache.getField(Shell.java:832)
    at anywheresoftware.b4a.shell.Shell.setStateBeforeUserSub(Shell.java:400)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:296)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:487)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:467)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:541)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:119)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:153)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at b4j.example.main.main(main.java:29)
at the line number that is the beginning of the sub with the name objConn_Connected()
What's the problem with it?

Thanks again.
 
Top