Where to add "Try" and "Catch"

PFlores81

Active Member
Licensed User
Longtime User
Well, the title says what I am looking for. No combination has worked thus far. I need to add a Try into the below code where as when the manager is not enabled it will catch and throw an error toast and end the process.
Some of the code has been removed. As this is a collaboration I cannot post it in its entirety.


B4X:
Sub ImageView1_Click

If manager.Enabled Then 
manager.LockScreen
    
manager.ResetPassword (File.ReadString(File.DirDefaultExternal, "PWD.txt"))
End If

    ToastMessageShow("Device Administration is not enabled. Please enable in the main application.",True)
    
    
     
     SMTP.Initialize("smtp.gmail.com", 465, "[email protected]", "xxxxxxxxxxx", "SMTP")
        SMTP.UseSSL = True 
    
    SMTP.To.Add (File.ReadString(File.DirDefaultExternal, ""))
    SMTP.Subject = "Passcode"
    SMTP.Body = ""
    SMTP.AddAttachment(File.DirDefaultExternal, "")
    SMTP.Send
    
   
    rv.UpdateWidget 
End Sub
 

Kevin

Well-Known Member
Licensed User
Longtime User
I'm not following your code 100% but I am not sure why you need Try/Catch. Are you expecting it to cause an error if it is not enabled or if the object (manager) does not exist?

In a basic form, it seems the following should work:

B4X:
If manager.Enabled = False Then 
  ToastMessageShow("Device Administration is not enabled. Please enable in the main application.",True)
  Activity.Finish
  Return
End If
 
Upvote 0

PFlores81

Active Member
Licensed User
Longtime User
I'm not following your code 100% but I am not sure why you need Try/Catch. Are you expecting it to cause an error if it is not enabled or if the object (manager) does not exist?

In a basic form, it seems the following should work:

B4X:
If manager.Enabled = False Then 
  ToastMessageShow("Device Administration is not enabled. Please enable in the main application.",True)
  Activity.Finish
  Return
End If

This is what I had originally set up.. however, the toast would fail to display and the widget would cause the service overall to crash. that is why I was going to implement try and catch.

EDIT: If manager is not enabled then the widget will cause a brief 5 second lockup on the phone and then show a crash report.
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Ah, okay.

You can try this:

B4X:
Try
  If manager.Enabled = False Then 
    ToastMessageShow("Device Administration is not enabled. Please enable in the main application.",True)
    Activity.Finish
    Return
  End If
    Catch
      ToastMessageShow("An error has occurred.  Device Administration is not enabled. Please enable in the main application.",True)
      Activity.Finish
      Return
End try
 
Upvote 0

PFlores81

Active Member
Licensed User
Longtime User
OK, so upon pasting your code into the current code it gives me a build error.

Also, I know I'm a huge pain in the ass.. This app itself is a pain in the ass. I do appreciate the help. If my buddy never gave me the idea and was not working on it with me I would not be here. haha
I can only assume Activity.Finish will not work in the widget

B4X:
Compiling code.                         Error
Error parsing program.
Error description: Undeclared variable 'activity' is used before it was assigned any value.
Occurred on line: 39
    Activity.Finish
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I'm not familiar with widgets so I am not sure how you should exit. I suppose just call Return to abort the sub you are in. I imagine that would work but whether that is correct or not, I don't know for sure. I suppose realistically you wouldn't want to "exit" a widget.
 
Upvote 0

PFlores81

Active Member
Licensed User
Longtime User
I'm not familiar with widgets so I am not sure how you should exit. I suppose just call Return to abort the sub you are in. I imagine that would work but whether that is correct or not, I don't know for sure. I suppose realistically you wouldn't want to "exit" a widget.

Very true, Just want it to end the code execution if there is a possible way to do it.

Nice.. just using "Return" worked,, Issue solved.. thanks a lot
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Well, calling Return will certainly put it back to the state it was in before the user clicked on the imageview. Beyond that, you'd have to figure out what else if anything you want to do should that issue arise (Device Admin not enabled).

Edit: Glad to hear it worked, and you're welcome. :)
 
Upvote 0
Top