Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Try-Catch - how does it execute?

Tags:

java

try-catch

I am just wondering what the Java VM does with a Try-Catch. How does it execute it?

My best guess is that it is like a Linux system which when installing something does a test run and if no errors are found asks the user if he/she wants to proceed. In the case of a Try-Catch does it do a test run and if all is OK implement it?

like image 891
user1360809 Avatar asked Jan 16 '23 03:01

user1360809


1 Answers

It is much simpler than that - if any clause included in the try clause generates an error, the code in the catch clause (corresponding to that error - you can have multiple catch for a single try) will be executed. There is no way to know in advance if a particular clause will fail or not, only to try to recover after the error happens.

If you have ten clauses and the last one throws an error, the modifications performed by the first 9 will not be "reverted"!

like image 52
thedayofcondor Avatar answered Jan 17 '23 18:01

thedayofcondor