I’m calling a method which involves lots of fetching and updating tables that might take 10+ minutes. This is done using a background thread so that the UI is responsive and the user doesn’t have to wait for it to complete.
There are chances that an exception might be raised. In that case, I need to udpate the status of a column from “Pending” to “Failed”.
Is it fine to do this in the catch block? Can I write a code in the catch block such that a query is executed to update the status to failed? Is this the right way or are there any other ways to do this?
Edit: Can I do something like this, so that when an exception is thrown, the status gets updated to “Failed” and the exception stack trace is printed out?
catch (Exception e) {
updateStatusByReqId(reqKey, "F");
e.printStackTrace();
}
Will this ensure that if there are any exceptions, the status of the request gets changed to “Failed”? Note that on creating the request online, the request will be having the status “Pending”. It should remain pending if everything is fine, otherwise changed to “Failed”
If you write the code to update the status in the catch block it will work fine, however since you are inside a method maybe could be better return a boolean value from the methos and update the ui from the caller of the thread, something like this :
Then you can call the method from the background thread and, log the status in the UI if it return false :
Doing in this way it is a better style, from my point of view …