Android Question Try Catch Problem

mangojack

Expert
Licensed User
Longtime User
Hi .. I am trying to download images from various sites using Job.GetBitmap.
Occasionally , Even with Job.Success True there is a problem with the Bitmap

for example .. what appears to be a single pixel image .. http://aucdn.ar-cdn.com/recipes/default.jpg

I attempted using Try / Catch but have hit a snag.

Question : If there is an error thrown in the Try block , do subsequent lines still execute ?
Log() lines don't appear in logs but the variable counter nSuccess will continue to increase ?
B4X:
If getImage.Success Then
   Try
      Dim imgThumb As ImageView
      imgThumb.Initialize("imgThumb")   
      imgThumb.Bitmap = getImage.GetBitmap  'Error here   on some bitmaps
               
      Log($"Successfully Downloaded Bitmap  #   ${nSuccess}"$)
       
      '@@@       This line still executes even with exception  ? ?  @@@
       nSuccess = nSuccess + 1
                       
    Catch       
       Log($"An ERROR Occured ...  current success tally = ${nSuccess}"$)  'File exists and downloaded OK .. But problem with Bitmap
    End Try

Else   
    Log($"Image download NOT successful ...   Image does not Exist"$)  'Error Responce 404 - File not Found
End If
getImage.Release

Or is there a better way to catch a bad bitmap?

Many Thanks
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
If there is an error thrown in the Try block , do subsequent lines still execute ?
The code in the catch block will be executed and then the lines after the End Try.

Your code is correct. The only thing that I recommend you to change is to use Job.GetBitmapSample instead of Job.GetBitmap.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Erel .. to clarify. Will subsequent lines in the Try block execute after an error exception is thrown. ie ..

B4X:
Try
    imgThumb.Bitmap = getImage.GetBitmap ' @   Error  Here !

    Log($"Successfully Downloaded Bitmap # ${nSuccess}"$)  '@@ Log lines do NOT execute

    nSuccess = nSuccess + 1    '@@@ This line Does execute even with exception

Catch

Thankyou
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Thankyou .. I will try to workout why this line appears to run.
nSuccess = nSuccess + 1 '@@@ This line Does execute even with exception
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I have created a small test project which i am hopeful someone can take a look at.

It attempts to download a single image and assign it to an ImageView. An error is thrown. "Unable to load bitmap .."

But why does Try block continue execute ?

Much appreciated for any help.
 

Attachments

  • TryCatch Test.zip
    6.9 KB · Views: 311
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
it behaves correctly in release and legacy debug mode
B4X:
Image download failed ... Bad Bitmap.  Success Count is  0

but with rapid debug mode indeed fails, you're right
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Error occurred on line: 48 (Main)
java.lang.RuntimeException: Error loading bitmap.
   at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:523)
   at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:501)
   at anywheresoftware.b4a.keywords.Common.LoadBitmap(Common.java:1308)
   at anywheresoftware.b4a.samples.httputils2.httpjob._getbitmap(httpjob.java:121)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
   at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
   at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
   at anywheresoftware.b4a.BA$2.run(BA.java:360)
   at android.os.Handler.handleCallback(Handler.java:815)
   at android.os.Handler.dispatchMessage(Handler.java:104)
   at android.os.Looper.loop(Looper.java:207)
   at android.app.ActivityThread.main(ActivityThread.java:5710)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
Image download failed ... Bad Bitmap.  Success Count is  1

So it seems as if there is some issue with try/catch blocks in resumable subs and/or rapid debug mode
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Thanks for that @JordiCP .. for this issue I did not try main / test projects in release or legacy .. which I'll do now.
Hopefully Erel will shed some light on this.

Again much appreciated Cheers.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
It is strange, because the log that is printed is the one of the Catch block, so at first glance would seem correct

But the content of the log states that the variable 'nSuccessCount' has been increased (in rapid debug mode).
So it seems as if the code in the 'Try' block continues before the 'Catch' is processed. Not the 'log' line but yes the next one.
B4X:
...
Image download failed ... Bad Bitmap. Success Count is 1   '<--where was it incremented?
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
But the content of the log states that the variable 'nSuccessCount' has been increased (in rapid debug mode).
So it seems as if the code in the 'Try' block continues before the 'Catch' is processed. Not the 'log' line but yes the next one.

I have not done any more tests.. but just to confirm, after the Error line , all Log() lines (multiple) were ignored .
There was only one more line in the Try block .. the one that incrememted nSuccess Count - this line was executed. see code post #3

Regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
@JordiCIP You are correct. The behavior in debug mode is not 100% correct here.

Resumable subs in debug mode are executed by the more sophisticated and slower pipeline which works by sending commands from the debug engine to the device app. It is the same pipeline that is used when there is a breakpoint in the relevant path or "dirty" code.

Exceptions are not detected immediately as it will kill the performance. Instead the device side knows that there was an exception and ignores the following commands until both sides are synchronized. This is why the log line is ignored.
Most commands are executed on the device however the incrementation line is executed on the PC.
 
Upvote 0
Top