Attention! This form is not ideal anymore. See documentation on.
Always look at the documentation of all the methods used to see which possible exceptions and decide which ones you can deal with effectively, that is, manage to solve the problem.
Environment.getExternalStorageDirectory()
indicates that there is no exception.
File()
can generate a NullPointerException
but this is a programming error and should not be dealt with specifically. There’s nothing that can be done to fix this at runtime, just fix the program.
deleteDirectory()
seems to be something of your code, so have to see inside it what can generate exceptions.
I wouldn’t wear one try-catch
there, but probably would use inside this deleteDirectory()
. There probably are other kinds errors that may be important to be treated.
In a larger context of the code I could change my mind and say that it has to deal with more generic exceptions. In general this should not be done in specific codes but there may be a case. It is almost certain that nay should do this there but there is case that capture a Exception
general may be useful.
Actually there is an abuse in the use of try-catch
, most of them in codes are not necessary, some hinder the functioning of the application or at least the detection of problems. In general people use it thinking it solves existing problems when in fact it only hides them. Other times they use it because they think it should be used without knowing why.
I talk about it in several answers here at Sopt. Read everything you can on the subject, even if it’s not about Java or Android. Not just what I wrote and the material found here on the site. It is very important to know how to use it correctly.
I edited the question but I don’t know if the question is very clear. It seems that the answer you accepted does not answer what you were actually asked. Could you clarify what you actually wanted to know? Even because this answer does not answer anything in fact and in the comment gives a bad advice.
– Maniero