Run block if no exception occurs

Asked

Viewed 126 times

3

I want to know if you can perform a function, a block, case NAY there was an exception in try {..}.

Example:

try {
    sout("Texto na tela");
} catch(Throwable t) {
    sout("Ocorreu uma exceção");
} casoNãoOcorraUmaExceção {
    sout("Não ocorreu uma exceção");
}

You know what I mean?

  • What’s the point, my dear? The last instruction within the Ry (immediately prior to the catch) will only be executed if no exceptions occur. So what you need is just this: try {
 sout("Texto na tela"); sout("Não ocorreu uma exceção");
} catch(Throwable t) {
 sout("Ocorreu uma exceção");
}

3 answers

3


I’m not sure if Java provides this feature directly, in any case you can use a controller variable that will indicate if there has been a success in try, in the finally (will always be executed at the exit of the try) you check the state of the variable:

boolean naoOcorreuExcecoes = false;

try {
    // Código para executar no bloco Try
    naoOcorreuExcecoes = true;
} catch(Throwable t) {
    // Fazer algo aqui caso ocorram exceções
}finally {
    if (naoOcorreuExcecoes){
        // Fazer algo aqui caso não ocorra exceções no Try
    }
}

2

The try/catch allows you to deviate the normal sequence of code execution when an exception occurs. If there is no exception, the execution follows the normal sequence.

try {
   sout("Texto na tela");
}catch(Throwable t) {
   sout("Ocorreu uma excepção");
   //Caso não queira que o código siga após o bloco catch
   return;
}
//Continua aqui caso não haja excepção 
sout("Não houve excepção");
..... 

Additionally exists, through the block declaration finally, the possibility of defining a code snippet that will always be executed whether or not there is an exception:

try {
   sout("Texto na tela");
}catch(Throwable t) {
   sout("Ocorreu uma excepção");
}
finally {  
   // Este bloco sempre será executado haja ou não excepção
}  

Therefore, and answering your question, you should use the code of the first example.

  • But in the first code, if an exception occurs it would also execute the Sout("There was no exception").

  • Put a return at the end of the block catch

  • I have the faint impression that this does not answer what was said in the question, he asks: how to run a block/function case no exception occurs in the block try. In the first code only is interrupted the normal sequence of execution with the return. In the second code, in the finally, whether there was an exception or not, something is done. AP asks one thing and wants another, it will understand. =)

  • 1

    @qmechanik The question does not, in fact, express what the PA intended. It was only clear when he comments on my reply, hence I edited and added the return. The question as it is asked does not make much sense because it is not necessary to do anything for the code to execute something when there is no exception, it follows the normal sequence. The question should be something like: "Run code block only when no exception occurs"

  • Can you explain why you changed your mind about the accepted question?

0

Have as you put the block Finally

 try {  
sout("Texto na tela");
    }  
    catch (Exception e) {  
  sout("Ocorreu uma excessão");
    }  
    finally {  
       // Faça alguma coisa aqui, esse bloco sempre será executado 


    } 
  • 1

    But the Finally block runs even when the exception occurs, and I want the block to run only if the exception does NOT occur.

Browser other questions tagged

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