Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:48
Show Gist options
  • Select an option

  • Save rcook/a0c1d8e56ed25bc937935fb4bc1bab89 to your computer and use it in GitHub Desktop.

Select an option

Save rcook/a0c1d8e56ed25bc937935fb4bc1bab89 to your computer and use it in GitHub Desktop.
Exception rethrowing and finally clauses
static boolean doOperationRethrowNotSkipped() {
try {
throw new RuntimeException();
} catch (Exception e) {
throw e;
} finally {
}
return true;
}
boolean doOperationRethrowSkipped() {
try {
throw new RuntimeException();
} catch (Exception e) {
throw e;
} finally {
return true;
}
}
doOperationRethrowSkipped: Action completes normally
(A) Before action
(B) After action
(D) In finally
result=true
doOperationRethrowSkipped: Action throws exception
(A) Before action
(C) In exception handler
(D) In finally
result=true
doOperationRethrowNotSkipped: Action completes normally
(A) Before action
(B) After action
(D) In finally
result=true
doOperationRethrowNotSkipped: Action throws exception
(A) Before action
(C) In exception handler
(D) In finally
Unhandled exception: java.lang.RuntimeException
The MIT License (MIT)
Copyright (c) 2019 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package org.rcook;
public class Main {
private static boolean doOperationRethrowSkipped(Runnable action) {
try {
System.out.println("(A) Before action");
action.run();
System.out.println("(B) After action");
} catch (Exception e) {
System.out.println("(C) In exception handler");
throw e;
} finally {
System.out.println("(D) In finally");
return true;
}
}
private static boolean doOperationRethrowNotSkipped(Runnable action) {
try {
System.out.println("(A) Before action");
action.run();
System.out.println("(B) After action");
} catch (Exception e) {
System.out.println("(C) In exception handler");
throw e;
} finally {
System.out.println("(D) In finally");
}
return true;
}
public static void main(String[] args) {
{
System.out.println("doOperationRethrowSkipped: Action completes normally");
try {
boolean result = doOperationRethrowSkipped(() -> {
});
System.out.format("result=%s%n", result);
} catch (Exception e) {
System.out.format("Unhandled exception: %s%n", e);
}
}
{
System.out.println("doOperationRethrowSkipped: Action throws exception");
try {
boolean result = doOperationRethrowSkipped(() -> {
throw new RuntimeException();
});
System.out.format("result=%s%n", result);
} catch (Exception e) {
System.out.format("Unhandled exception: %s%n", e);
}
}
{
System.out.println("doOperationRethrowNotSkipped: Action completes normally");
try {
boolean result = doOperationRethrowNotSkipped(() -> {
});
System.out.format("result=%s%n", result);
} catch (Exception e) {
System.out.format("Unhandled exception: %s%n", e);
}
}
{
System.out.println("doOperationRethrowNotSkipped: Action throws exception");
try {
boolean result = doOperationRethrowNotSkipped(() -> {
throw new RuntimeException();
});
System.out.format("result=%s%n", result);
} catch (Exception e) {
System.out.format("Unhandled exception: %s%n", e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment