Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Display of runtime exception in postConstruct method(How to display runtime exception in postconstruct method in a Eclipse RCP e4 application?)
Display of runtime exception in postConstruct method [message #929112] Mon, 01 October 2012 08:29 Go to next message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member
I am working on a Eclipse E4 RCP application and I use Dependency Injection.

I have noticed that when there is a runtime exception in the postconstruct, then I don't see that Exception in the console (is it swallowed?). I do see an exception that that class can't be injected.

So for example the following code:

    @Creatable
    @Singleton
    public class OurClass {
        @PostConstruct
        public void postConstruct() {
            System.out.println("PostConstruct Start");
            throw new RuntimeException("Test");
        }
    }

    public class OurPart {
    	@Inject
    	private OurClass ourClass;
    }


Gives the following output:

    PostConstruct Start
    org.eclipse.e4.core.di.InjectionException: Unable to process "OurPart.ourClass": no actual value was found for the argument "OurClass".
    	at org.eclipse.e4.core.internal.di.InjectorImpl.reportUnresolvedArgument(InjectorImpl.java:392)
    	at org.eclipse.e4.core.internal.di.InjectorImpl.resolveRequestorArgs(InjectorImpl.java:383)
    	at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:100)
    	at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:318)
    	at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:240)
    	at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:161)
    	at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:102)
    	at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:71)
    	at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:53)
    	at org.eclipse.e4.ui.internal.workbench.E4Workbench.processHierarchy(E4Workbench.java:170)
    	at org.eclipse.e4.ui.internal.workbench.E4Workbench.init(E4Workbench.java:118)
    	at org.eclipse.e4.ui.internal.workbench.E4Workbench.<init>(E4Workbench.java:69)
    	at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createE4Workbench(E4Application.java:302)
    	at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:132)
    	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    	at org.eclipse.equinox.internal.app.MainApplicationLauncher.run(MainApplicationLauncher.java:32)
    	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
    	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    	at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
    	at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
    PostConstruct Start


Does anyone know how to make sure the runtime exception is also shown?
Re: Display of runtime exception in postConstruct method [message #929152 is a reply to message #929112] Mon, 01 October 2012 09:03 Go to previous messageGo to next message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
What happens if you make the injection @Optional
Re: Display of runtime exception in postConstruct method [message #929228 is a reply to message #929112] Mon, 01 October 2012 09:55 Go to previous messageGo to next message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member
Phill,

I made all injections of "ourClass" optional. When I run the application I get a nullPointerException the first time I use that class.
Which makes sense, because the class was never created.

I still don't see the RuntimeException for creating ourClass.
Re: Display of runtime exception in postConstruct method [message #929236 is a reply to message #929228] Mon, 01 October 2012 10:00 Go to previous messageGo to next message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
There may be some way og getting the system to throw it I don't know but you could change OurClass

add a static field and a static getter for it
RuntimeException rte

Update the constructor

rte = new RuntimeException(test);
throw rte

The you can test in your main app
if (ourClass == null)
throw OurClass.getRte();

and throw the error yourself.

Not pretty, but...
Re: Display of runtime exception in postConstruct method [message #929256 is a reply to message #929236] Mon, 01 October 2012 10:15 Go to previous messageGo to next message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member
Hi Phill,

Thanks for your suggestion. To do that I would really change a lot of code, just for the case that an exception is occurring. It is not that I am expecting some kind of exception.

I would like to see where I broke my class, if I break it. Now I only see that I broke ourClass and not why. At the moment I set a breakpoint at the constructor and walk though the code to see where it is going wrong. But I was hoping to display the exception at the injection, so I don't have to walk through the code when that happens.
Re: Display of runtime exception in postConstruct method [message #929361 is a reply to message #929256] Mon, 01 October 2012 11:47 Go to previous message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member
I did found a workaround, similar to Phill's solution.

I wrapped my code with a try/catch block, which explicitly prints the stack trace.

	@PostConstruct
	public void postConstruct() {
		System.out.println("OurClass PostConstruct Start");

		// Workaround to make sure exception is displayed when thrown
		try{
			methodThatCouldThrowRuntimeException();
		}
		catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}

		System.out.println("OurClass PostConstruct End");
	}


I am not happy with it, but it works for now. So if anybody knows how to display those exceptions by default, then I like to hear it.
Previous Topic:Editors in 4x
Next Topic:.e4css Folder
Goto Forum:
  


Current Time: Tue Apr 23 16:39:31 GMT 2024

Powered by FUDForum. Page generated in 0.03663 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top