What exceptions should I capture in a Try-catch?

Asked

Viewed 841 times

1

I have some doubt in try catch in relation to which types of exceptions put. In this example either would be the best option?

try {
    String folerPath = Environment.getExternalStorageDirectory() + File.separator + "Folder" + File.separator;
    File dir = new File(folderPath);
    deleteDirectory(dir);
} catch (Qual exceção?){

}
  • 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.

2 answers

9

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.

0


Friend, if you need to treat the exceptions separately, I advise to use chained Try catch, follow an example below:

try {
// código

}catch (Excecao1 ex) {
//Tratamento da excecao 1
}

catch (Excecao2 ex) {
//Tratamento da excecao 2
}

If you don’t need to treat separately, just use the generic class of exceptions.

  • do so?: catch (Exception e){ e.getMessage(); }

  • Yes, if you want more information use e.printStackTrace()

  • Ah, if your code throws only one type of exception, use the respective exception class.

  • 3

    In fact even if throw one more type of exception one should take care not to capture the generic exception Exception, that can mask unforeseen errors. See @bigown’s reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.