Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does exception handling rollback actions done before the exception occurred?

Imagine I have a method like this

void myMethod(MyThing t) throws MyException {
   t.foo = "bar";
   if (t.condition()) { 
      throw new MyException();
   }
}

If the exception is triggered, does the value of t.foo revert to whatever it was previously? Or does it keep the "bar" value?

like image 828
interstar Avatar asked Jun 28 '11 04:06

interstar


People also ask

What is rollback exception?

RollbackException exception is thrown when the transaction has been marked for rollback only or the transaction has been rolled back instead of committed. This is a local exception thrown by methods in the UserTransaction , Transaction , and TransactionManager interfaces.

When error happens in exception handling it can be recovered?

The exception handler can attempt to recover from the error or, if it determines that the error is unrecoverable, provide a gentle exit from the program. Three statements play a part in handling exceptions: The try. statement identifies a block of statements within which an exception might be thrown.

What are the steps involved in exception handling?

It can consist of 3 steps: a try block that encloses the code section which might throw an exception, one or more catch blocks that handle the exception and. a finally block which gets executed after the try block was successfully executed or a thrown exception was handled.

What is exception handling and how does it work?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.


1 Answers

The value of the foo property on your MyThing object will not revert on any Exception.

In your example, there is no try block, but if there were one, you could perform your own type of rollback of the value in a corresponding catch block.

try {
    t.foo = "bar";
    doSomethingRiskyWhichMightThrowMyException();
} catch(MyException e) {
    t.foo = "rolledbackvalue";
}
like image 89
nicholas.hauschild Avatar answered Sep 27 '22 21:09

nicholas.hauschild