Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 346827

Summary: [Xtend] support for throws-clause
Product: [Modeling] TMF Reporter: Serano Colameo <serano.colameo>
Component: XtextAssignee: Jan Koehnlein <jan>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: jan, sven.efftinge, tmf.xtext-inbox
Version: unspecifiedFlags: sven.efftinge: juno+
Target Milestone: M4   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Bug Depends on:    
Bug Blocks: 363937    

Description Serano Colameo CLA 2011-05-23 03:10:45 EDT
Build Identifier: Xtend2 SDK	2.0.0.v201105171404

xtend2:
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
	....
		// auf ruf einer method die eine exception throwed
		// z.B. SQLException
		throw new MyException()
	}

java:
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) throws IllegalArgumentException, MyException {
    {

Bug: of course, an overwritten method cannot throw more exception as defined in the super-class



Reproducible: Always
Comment 1 Sven Efftinge CLA 2011-05-23 03:31:59 EDT
Workaround : Catch exceptions
Comment 2 Sven Efftinge CLA 2011-09-26 05:55:20 EDT
For SR2, at least a validation rule, telling the user, that a certain exception must be caught, would be good. We should also allow having a throws clause (post SR2).
Comment 3 Jan Koehnlein CLA 2011-11-15 12:00:07 EST
First shot pushed to MASTER.


I've uncommented 
CompilerTest.testRethrownCheckedExceptions_00()
CompilerTest.testRethrownCheckedExceptions_01()
Comment 4 Jan Koehnlein CLA 2011-11-15 12:06:44 EST
Xtend functions now can declare thrown exceptions, which are compiled to Java, too.
Checked exceptions in function bodies must be declared.
Declared exceptions from overriden functions are validated.
Checked exceptions thrown inside a function are validated against the declared ones.

In Xbase, closures can no longer throw checked exceptions. Validation ensures this.

This is a semantic change in Xtend, so please revise. I've uncommented 
  CompilerTest.testRethrownCheckedExceptions_00()
  CompilerTest.testRethrownCheckedExceptions_01()
which test that checked exceptions are rethrown by the callers (which is no longer the case). If we agree on these changes, I'm going to clean up the compiler stuff for rethrowing.
Comment 5 Sven Efftinge CLA 2011-11-16 02:41:12 EST
Looks good. 
Please clean up the part in the compiler you mentioned.
A quick fix for adding a throw-clause resp. a try-catch would be nice as well.
Comment 6 Jan Koehnlein CLA 2011-11-16 09:16:18 EST
Cleaned up rethrow in compiler and added quickfix for "Add throws declaration"
Comment 7 Jan Koehnlein CLA 2011-11-16 10:38:51 EST
Pushed quick fix "sourround with try/catch"
Comment 8 Sven Efftinge CLA 2011-11-16 12:12:29 EST
I changed my mind (sorry for that)

I think we should deactivate the check until we have preferences, so that users can deice whether they want to have an error, warning or nothing at all for unchecked exceptions.

The compiler should always be able to compile and use the sneaky throw technique from Lombok, whenever a checked exception wasn't declared but might be thrown. 

https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/Lombok.java
Comment 9 Sven Efftinge CLA 2011-11-17 03:36:59 EST
The validation for checked exceptions is activated in Xbase by default and set to inactive for Xtend.
As soon as we have preferences the User should decide on that (still default should be 'ignore').

I added the mentioned sneakyThrow to the compiler, such that regardless of the validation the compiler is always able to generate valid Java code.

The quick fixes and the like are still all very valuable but we shouldn't add additional ones for now (like add catch clause to existing try-catch).
Comment 10 Jan Koehnlein CLA 2011-11-21 05:19:02 EST
Quick fixes work fine when validation for checked exceptions is enabled.
Comment 11 Karsten Thoms CLA 2017-09-19 18:03:36 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 12 Karsten Thoms CLA 2017-09-19 18:13:45 EDT
Closing all bugs that were set to RESOLVED before Neon.0