Always look at the documentation of all methods used to see what the possible exceptions are and decide which you can handle effectively, that is, you can solve the problem.
Environment.getExternalStorageDirectory()
indicates no exception.
File()
can generate NullPointerException
however this is a programming error and should not be treated in a specific way. There is nothing that can be done to solve this at runtime, just fix the program.
deleteDirectory()
seems to be something from your code, so having to see inside it can lead to exceptions.
I would not use a try-catch
there, but would probably use within this deleteDirectory()
. There are probably other types of errors that can be important.
In a larger context of the code I could change my mind and say that I have to handle more generic exceptions. In general this should not be done in specific codes but there may be case. It's almost certain that not should do this there but there is a case that capturing a Exception
overall can be useful.
In fact there is an abuse of try-catch
, most of them in codes are not required. Some hinder the operation of the application or at least the detection of problems. In general people use it thinking that this solves existing problems when in fact it only hides them. Sometimes they use it because they think it should be used without knowing why.
I talk about this in various answers here at SOpt . Read everything you can about it, even if it's not about Java or Android. Not only what I wrote and the material found here on the site. It is very important to know how to use this correctly.