B4J Question jPOI crash when reading Workbook with wrong password

Hi,

Is there a way around the crash you get if you send wrong password during opening of a workbook with password ?

B4X:
Dim excel_wb As PoiWorkbook

'Read excel file
excel_wb.InitializeExisting("", FileName, "password")

If there is no password on the workbook then it opens fine.
If the password is correct then the workbook opens fine.
If the password is not correct then application crashes as below.
It would have been nice to be able to check if the password was incorrect and not crash.

org.apache.poi.EncryptedDocumentException: Password incorrect
at org.apache.poi.poifs.filesystem.DocumentFactoryHelper.getDecryptedStream(DocumentFactoryHelper.java:88)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:108)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:74)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:212)
at anywheresoftware.b4j.objects.PoiWorkbookWrapper.InitializeExisting(PoiWorkbookWrapper.java:53)
at b4j.example.main._gskloadmaterialfromxlsx(main.java:3286)
at b4j.example.main._gskloadpricelistbutton_click(main.java:3544)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$1.run(BA.java:216)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)


/Christian
 
Solution
B4X:
Try
 excel_wb.InitializeExisting("", FileName, "password")
Catch
 Log("Failed to open workbook: " & LastException.Message)
End Try
Top