I am a computer science student therefore I do not know that much.
I was recently talking with a friend who just got a job as a (java) software developer. He told me that in his job there is a guy who is really experienced in C++, but unfortunately every time he writes code in java, he is using the try-catch to control the flow of the program. According to my friend this is a wrong style in Java. Is this true? What are the differences (if any) in using try-catch(-finally in java) between C++ and Java?
Using try-catch to control the flow of the program is wrong anywhere… Exception handling is what it says it is: Handling of exceptional circumstances.
Of course for every rule there are a dozen counter-examples of necessary deviations, but generally speaking: Don’t control program flow with exceptions.
Using exceptions for controlling the flow of a program occurs when you anticipate certain exceptions being thrown in a normal operating environment, and you make logical decisions based on those exceptions.
For example controlling program flow in pseudo code:
In this case it would be better to actually test for path existence before blindly performing the write.
I removed the permission checking notes because it’s a bad example
A good usage of exception handling: (pseudo code again)