Page 1 of 1

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Thu May 07, 2015 3:24 am
by Alina_Lapina
Is it possible to get more then one exception suppressed?

I've tried the following code:

Code: Select all

public class Onion implements AutoCloseable{
	public void m1() throws Exception{ throw new Exception("Exception from m1");} 
	
	public void m2() throws Exception{
		try (Onion o = new Onion()) {o.m1();}
		catch(Exception e) { throw e;}
		finally{ throw new RuntimeException("Exception from finally");}
	}
	
	public static void main(String[] args) {
		try (Onion o = new Onion()){o.m2();} 
                catch(Exception e){
			Throwable[] ta = e.getSuppressed();
			for(Throwable t : ta)
				System.out.println(t.getMessage());
		}
	}

	@Override
	public void close() throws Exception {
		throw new RuntimeException("Exception from close");		
	}
and it still only one exception is suppressed -- the one from the close() method.

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Thu May 07, 2015 5:26 am
by admin
You can try wrapping this whole code in the close method of another class. That should suppress the exception that you are getting right now, which internally already has one suppressed exception. Haven't tried it myself but you can :)

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Thu May 07, 2015 5:28 am
by admin
Another option is to generate exceptions yourself in try catch blocks and in the catch blocks, keep adding the exception to other suppressed exception using the addSuppressed(Throwable exception) method.

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Tue Jan 10, 2017 5:38 am
by jagoneye

Code: Select all

public class AddSuppressed {    
    public static void m1() throws Exception{
        throw new Exception("Exception from m1");
    }
    public static void m2() throws Exception{
        Throwable t = null;
        try{
            m1();
        }catch(Exception e){
            //Can't do much about this exception so rethrow it
            t = e;
        }finally{            
            Exception e = new RuntimeException("Exception from finally");
            e.addSuppressed(t);
            throw e;
        }
        
    }
    
    public static void main(String[] args) {
        try{
            m2();
        }catch(Exception e){
            System.out.println(e);
            Throwable[] ta = e.getSuppressed();
            for(Throwable t : ta) {
                System.out.println(t.getMessage());
            }
        }
    }   
}
Output:

Code: Select all

java.lang.RuntimeException: Exception from finally
Exception from m1
This is the best I could do to demo addSuppressed method.
Also addSuppressed() and getSuppressed are new features of Java 7!
Hope this helps! :)

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Tue Apr 13, 2021 1:38 pm
by minajev3
Hi! Can you please explain why in general if we have exception in catch and finally we get just exception from finally and exception from catch is just gone.

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Tue Apr 13, 2021 7:37 pm
by admin
A method can throw only one exception, so, as a language designer you have to decide which one you want the method to throw - the one thrown in the catch block or the one thrown in finally. Unless, you want the method to be able to throw multiple exceptions at the same time.

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Sat Apr 17, 2021 10:48 am
by jme_chg
is it even possible to get a suppressed exception with a normal try-catch? cannot think of a way that this is possible?

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Sat Apr 17, 2021 10:57 am
by admin

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Sun Feb 04, 2024 10:27 am
by Badem48
I think this is a good question; however, I believe an important point is missing or not clear in the explanation:
The suppressed exceptions apply only to exceptions thrown in the try clause.

It is explained that the getSuppressed() method returns an empty array, but the reason why is not included.

Thanks,

Re: About Question enthuware.ocpjp.v7.2.1713 :

Posted: Sun Feb 04, 2024 9:24 pm
by admin
The first para of the explanation already explains why:
the given code, method m2() throws an exception explicitly from the catch block as well as from the finally block. Since this is an explicit finally block (and not an implicit finally block that is created when you use a try-with-resources statement), the exception thrown by the finally block is the one that is thrown from the method. The exception thrown from the catch block is lost. It is not added to the suppressed exceptions list of the exception thrown from the finally block.