I am quite new to Java and Android development. I looked at the code of AsyncTask
, and saw the class throws 3 Exceptions:
InterruptedException
ExecutionException
TimeoutException
When I run the execute method on an AsyncTask
object, why is it that the compiler complains if I don't catch InterruptedException
and ExecutionException
, but does not complain about TimeoutException
?
More generally, how do we know which exceptions need to be caught? (Of course I look at the compiler errors and write the missing catch blocks, but I'd like to understand the principal behind it).
Thank you very much!
Example code:
public void test() {
AsyncTask at = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
return null;
}
};
Object o;
try {
o = at.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
Edit: I checked that TimeoutException
is a checked exception (which should be caught). However @NicolasFilotto already answered my question. Thank you all.