📜 ⬆️ ⬇️

Features Exception Handling

Some things sometimes do not work as intuition suggests. This statement can be attributed to exception handling in Java. Next are situations and code examples that reflect some of the existing nuances.


The simplest case, the exception happens:

public class Test01 {
static int doTest() throws Exception {
for ( int i = 0; i < 10; i++) {
System.out.println( "i = " + i);
if (i == 3) {
throw new Exception();
}
}
return -1;
}
public static void main(String[] args) throws Exception {
System.out.println( "doTest() = " + doTest());
}
}

Everything is simple and intuitive.

Exceptions happen, but we catch them:

public class Test02 {
static int doTest() {
for ( int i = 0; i < 10; i++) {
System.out.println( "i = " + i);
try {
if (i == 3) {
throw new Exception();
}
} catch (Exception e) {
System.out.println( "Exception!" );
return i;
} finally {
System.out.println( "Finally block" );
}
}
return -1;
}
public static void main(String[] args) {
System.out.println( "doTest() = " + doTest());
}
}

Here, too, everything is elementary. The finally block executes at each iteration, and after catching an exception, that is, in principle, always, there was an exception or not, whether we left the method or not.
')
Exceptions happen, we catch them and ... cancel:

public class Test03 {
static int doTest( int n) {
for ( int i = 0; i < n; i++) {
System.out.println( "i = " + i);
try {
if (i % 3 == 0) {
throw new Exception();
}
} catch (Exception e) {
System.out.println( "Exception!" );
return i;
} finally {
System.out.println( "Finally block" );
if (i % 3 == 0) {
if (i < 5) {
System.out.println( "Cancel exception, please" );
continue;
} else {
System.out.println( "OK, now everything is done" );
return 42;
}
}
}
}
return -1;
}
public static void main(String[] args) {
System.out.println( "doTest(2) = " + doTest(2));
System.out.println();
System.out.println( "doTest(10) = " + doTest(10));
}
}


But this option shows the moment at which I fell asleep about three months ago at an interview:
i = 0
Exception!
Finally block
Cancel exception, please
i = 1
Finally block
doTest(2) = -1

i = 0
Exception!
Finally block
Cancel exception, please
i = 1
Finally block
i = 2
Finally block
i = 3
Exception!
Finally block
Cancel exception, please
i = 4
Finally block
i = 5
Finally block
i = 6
Exception!
Finally block
OK, now everything is done
doTest(10) = 42

The finally block is always executed, in addition, it can override the result of the return from the method, or cancel the return.

And even with multithreading?

import java.util.concurrent.*;
public class Test04 implements Runnable {
public void run() {
try {
for ( int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread() + ": " + i);
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
System.out.println( "Interrupted!" );
} finally {
System.out.println( "I'm in the finally block!" );
}
}
public static void main(String[] args) throws Exception {
Thread t = new Thread( new Test04());
t.start();
TimeUnit.SECONDS.sleep(5);
System.out.println( "main() finished" );
}
}

Seems to be yes:
Thread[Thread-0,5,main]: 0
Thread[Thread-0,5,main]: 1
Thread[Thread-0,5,main]: 2
Thread[Thread-0,5,main]: 3
Thread[Thread-0,5,main]: 4
main() finished
Thread[Thread-0,5,main]: 5
Thread[Thread-0,5,main]: 6
Thread[Thread-0,5,main]: 7
Thread[Thread-0,5,main]: 8
Thread[Thread-0,5,main]: 9
I'm in the finally block!


And now - cookies!

import java.util.concurrent.*;
public class Test05 implements Runnable {
public void run() {
try {
for ( int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread() + ": " + i);
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
System.out.println( "Interrupted!" );
} finally {
System.out.println( "I'm in the finally block!" );
}
}
public static void main(String[] args) throws Exception {
Thread t = new Thread( new Test05());
//
t.setDaemon( true );
t.start();
TimeUnit.SECONDS.sleep(5);
System.out.println( "main() finished" );
}
}

Does not work!
Thread[Thread-0,5,main]: 0
Thread[Thread-0,5,main]: 1
Thread[Thread-0,5,main]: 2
Thread[Thread-0,5,main]: 3
Thread[Thread-0,5,main]: 4
main() finished
Thread[Thread-0,5,main]: 5


Even to the normal completion of the program, in the streams of the type “daemon” the finally block does not work.

And yes, as noted , when you exit the try-catch block through System.exit (0), the finally block also does not work.
up: instead of System.exit (int) you can use Runtime.getRuntime (). halt (int)

My original

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


All Articles