About Question enthuware.ocpjp.v7.2.1713 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Alina_Lapina
Posts: 15
Joined: Tue Jan 13, 2015 12:10 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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 :)
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post 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! :)

minajev3
Posts: 18
Joined: Fri Feb 05, 2021 3:37 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

If you like our products and services, please help us by posting your review here.

Badem48
Posts: 26
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

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

Post 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,

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests