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?
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.
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.
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.
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.
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With