Why is it impossible to insert code between a try and catch block?

I was asked in an interview what will happen if we put the final block between try and catch block, to which I answered, in this case the compiler will think that there is no catch block and he will immediately execute the final block. He then asked why it is not possible to put code between a try and catch block?

Could you help me...

+6
source share
5 answers

Well, first the first - the compiler does not execute the code, it just compiles it, allowing it to run the JVM.

Empirically speaking, this will not make much sense, because if you have code that you want to put outside the try block, but before the catch block, the code can also be placed in a try block. The fact is that in any case, it will behave as if in a try block, if you think about it.

Suppose this is valid Java ( this does not compile ):

try { throw new Exception(); } System.out.println("Sup!"); catch(Exception e) { } 

When an exception is thrown, this is the line that displays Sup! will still be skipped when the JVM searches to go to the appropriate exception handler to handle the Exception . So, in a way, the code behaves as if it were in the try {} block itself, so it really doesn't matter where it is, and Java indicates that this (now proven useless) construct is illegal.

Now, what if this code had to throw another exception after the attempt? If it was valid code, it will behave exactly like the nested try ... catch block in the original try block. Of course, when the situation becomes more complicated, an approach in which there is no clear connection between the attempt and the catch block may become fuzzy, and the JVM will eventually not know which catch / finally the try block belongs to (especially since the handler should not be in the same function or even in one package!).

+11
source

Well, that would mean something like this:

 try { somCode(); } someMoreCode(); catch { } 

What does it mean? This is impossible because it has no semantics, so it was decided that the language developers would be syntactically incorrect!

+4
source

Well, the frivolous answer is that the language specification forbids it.

But let me step back a little and think about it differently - what if you can do it?

 try { foo(); } bar(); catch (Exception e) { baz(); } 

What could be the semantics of this? If we catch the exception in foo() , is baz() called? What about bar() ? If bar() throws, will we catch an exception in this case?

If the exceptions in bar() not caught, and the exception in foo() prevents the execution of bar() , then the construction is equivalent:

 try { foo(); } catch (Exception e) { baz(); } bar(); 

If the exceptions from bar() caught, and the exception in foo() prevents the execution of bar() , then the construction is equivalent:

 try { foo(); bar(); } catch (Exception e) { baz(); } 

If exceptions do not fall into bar() , and an exception to foo() does not prevent bar() from executing ( bar() always executed), then the construction is equivalent:

 try { foo(); } catch (Exception e) { baz(); } finally { bar(); } 

As you can see, any reasonable semantics for this construct between try-catch are already expressed without the need to create a new - and rather confusing - construct. It is difficult to come up with meaning for this design, which is no longer redundant.

As one of the parties, we cannot make one possible reason:

 try { foo(); } finally { bar(); } catch (Exception e) { baz(); } 

it may be that it does not reflect the actual order of execution - blocking blocks run before the finally block. This allows catch blocks to use resources that the last block can release later (for example, request additional diagnostic information from an RPC object or something else). Is it possible to make it work like that? Of course. Is it worth it? Probably not.

+4
source

If you have try , you must have either catch or finally , as defined in the java language specification, 14.20 "Statement of attempt`

0
source

try and catch how .. if and more .. so there is no need to add code between the try and catch block

0
source

Source: https://habr.com/ru/post/920188/


All Articles