I am following an example of the popular Ion library that Koush created: Download a File with a progress bar and I can see from one of his samples that it is possible to send a cancellation using downloading.cancel().
NB: downloading was declared as Future<File>.
When the cancel() method is called it throws a CancellationException. I would like to handle this exception specifically, instead of catching it generally.
My first thought is to wrap a try / catch block, and use something like:
try {
//...
} catch (CancellationException ce) {
//...
}
However if you look at his example, you will see that the onCompleted callback uses 2 inputs: Exception e and File result.
The sample here (line 72) shows this:
if (e != null) {
Toast.makeText(ProgressBarDownload.this, "Error downloading file", Toast.LENGTH_LONG).show()
return;
}
This appears to be an alternative to a try / catch block which is generalising an error if an exception exists. However my aim to return a different toast message if the CancellationException was caught.
To handle exceptions individually we can look for the exception type in the instance of the thrown exception
e.This is the solution that works for me: