What is the best method for exceptions?

Asked

Viewed 467 times

2

What’s the best method to pick up exceptions in an Android app, like try/catch, throws or throw.

Type exceptions, fields the user left blank in a form.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

7

There is no better "method" to capture exceptions, there is only one way:

try {
    //faz coisas aqui
} catch (SpecificException ex) {
    //faz algo para se recuperar da falha ou indicar o erro em log ou tela
    // só entrar aqui se uma exceção deste tipo ou derivada dela for lançada no bloco try
}

It is possible to have several catch, and must always capture the most specific exceptions first and the more generic ones later. Avoid capturing Exception anywhere. Usually it should be the last measure when everything failed.

It is also possible use finally to ensure that something runs regardless of whether an exception has been made or not. It is usually used to close some open resource, such as database. More about this in C# which is the same thing.

try {
    //faz coisas aqui
} catch (SpecificException ex) {
    //faz algo para se recuperar da falha ou indicar o erro em log ou tela
    // só entrar aqui se uma exceção deste tipo ou derivada dela for lançada no bloco try
} catch (OtherSpecificException ex) {
    //faz algo para se recuperar da falha ou indicar o erro em log ou tela
    // só entrar aqui se uma exceção deste tipo ou derivada dela for lançada no bloco try
} catch (SpecificPeroNoMuchoException ex) {
    //faz algo para se recuperar da falha ou indicar o erro em log ou tela
    // só entrar aqui se uma exceção deste tipo ou derivada dela for lançada no bloco try
} catch (Exception ex) {
    //Indicar o erro em log ou tela
    // só entrar aqui se uma exceção deste tipo ou derivada dela for lançada no bloco try
} finally {
    //aqui executa no final lançando exceção ou não
}

I put in the Github for future reference.

But it’s enough to avoid the use of exceptions in many cases. Do not capture exceptions that are generated by programming errors. Fix the error. I find it regrettable that the answer in that question that does this has almost the same number of votes as the answer (mine) that solves the problem without letting the exception occur. Do not use exceptions to say that the user left the field blank. This is validation and no exception. There are other ways to avoid the exception. In general, exceptions should be only in exceptional cases and not control the normal program flow. For this there is the if.

Think before you capture an exception, don’t capture it if you’re not sure you can do something useful there. Have a general catch to grab everything when you forgot to capture something more specific in a more specific location.

See example.

throw serves to cast the exception and throws to mark a method that there is an exception there and that it needs to be captured. Learn more.

More on the subject. Or search the tag on the subject.

2

To validate unfilled fields, you will not be able to do this with try catch because the empty field will not generate an exception.

Use the following command to check that the field is empty:

if (myEditText.getText().toString().trim().equalsIgnoreCase("")) {
  // CAMPO ESTÁ VAZIO
}

Or:

if (myEditText.getText().toString().trim().length() == 0) {
  // CAMPO ESTÁ VAZIO
}

Or:

if (myEditText.getText().toString().isEmpty()) {
  // CAMPO ESTÁ VAZIO
}

I hope I’ve helped.

1

Good morning friend :D
Tbm am new in programming and am focusing on Android
in the project I’m doing agr I used "if" which works as excel "SE" function. You give one condition and he does something if true, and if false do something else
ex:

    if(condição){  
    //Codigo se for verdadeiro  
    } else {  
    //codigo se falso  
    }

what I did check empty fields and stayed

    if (etA.gettext().toString().equals(""){   //verifica se o campo é vazio  
    //codigo do programa  
    } else { //se falso  
    Toast.makeText(getApplicationContext(), “Preencha o campo vazio”, Toast.LENGTH_SHORT).show();
    }

I hope I helped :D

  • All you need to do is switch places with Toast, which in your case has to be on if and not Else.

Browser other questions tagged

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