0

I am reviewing for OCP and I stumbled upon this scenario with Exceptions.

Typically, we encounter Suppressed Exceptions in try-with-resource. if the try block and close() method both throws an Exception, only the one in try block will be handled. The exception thrown in close() will be suppressed.

I am experimenting other ways to encounter suppressed exceptions. Running methodTwo() will just throw NullPointerException. It will be catched but it is not suppressed. What happened to IllegalArgumentException?

    public class Main {
       public static void main(String[] args) {
        try {
            methodTwo();
        } catch (Exception e) {
            e.printStackTrace();
            for(Throwable t : e.getSuppressed()) {
                System.out.println(t.getMessage());
            }
        }
    }


    static void methodTwo() {
        try {
            throw new IllegalArgumentException("Illegal Argument");
        } finally {
            throw new NullPointerException("Null Pointer"); 
        }
    }
  }
EDR
  • 277
  • 1
  • 2
  • 13

2 Answers2

0

as mentioned by comments, finally always executed if any exception or return happen. it is because of assurance of free resource like files and etc. if you don't return or throw new exception in finally, it return exception or value that set before. you can change value that return in finally block too for example:

class A
{
    public int value; // it is not good but only for test
}

public class Tester
{
    public static void main(String[] args) {
        System.out.println(method1().value); // print 10
    }

    private static A method1() {
        A a = new A();
        try
        {
            a.value = 5;
            return a;
        } finally
        {
            a.value = 10;
        }
    }
}

you can throw exception instead of throwing new value too and return value or last exception discarded. (but all of this is not good in programming design)

when you working with files, because there is nothing like destructor in java like c++ (although there is finally but it is different) you must using try finally (or for new way, use try-with-resource) to free resource obtained from system.

Amin
  • 1,643
  • 16
  • 25
-2

As explained here by @polygenelubricants

A try statement with a finally block is executed by first executing the try block. Then there is a choice:

  • If execution of the try block completes normally, [...]
  • If execution of the try block completes abruptly because of a throw of a value V, [...]
  • If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:
    • If the finally block completes normally, [...]
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

For more detail , go to oracle documentation

Community
  • 1
  • 1
yash
  • 2,101
  • 2
  • 23
  • 32
  • @ScaryWombat, I also marked it as duplicate too. So, may be as per you we don't have to give credit to person and reference link when copying answer. then it will not be identified as copy paste. correct? – yash Oct 03 '18 at 08:25