What is the best method for exceptions?

2

What is the best method to catch exceptions in an Android app, type try/catch , throws or throw .

Type exceptions, fields that the user left blank on a form.

    
asked by anonymous 25.09.2015 / 07:18

3 answers

6

There is no better "method" for catching 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 multiple catch , and you should always capture the most specific exceptions first and the most afterwards. Exit capture Exception anywhere. Normally it should be the last step when all else fails.

You can also use finally to ensure that something runs regardless of whether an exception was thrown or do not. Usually used to terminate 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
}

But you can avoid exceptions in many cases . Do not catch exceptions that are generated by programming errors. Fix the error. I find it unfortunate 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 rather than exception . There are other ways to avoid the exception . In general, exceptions should only be in exceptional cases and do not control the normal flow of the program. For this there is if .

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

See example .

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

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

25.09.2015 / 13:36
2

For you 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 commands to check if 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 have helped.

    
25.09.2015 / 14:01
1

Good morning friend: D
I'm new at programming and I'm focusing on Android
in the project I'm doing agr I used "if" which functions as the "function" of excel. You give a condition and it 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 have helped: D

    
28.09.2015 / 15:18