DMD special exception "catch not allowed in finally block"

Bradley Smith digitalmars-com at baysmith.com
Fri Jun 23 15:14:30 PDT 2006


Why does DMD have this special exception for the finally block?

The documentation states that "A FinallyStatement may not exit with a 
throw, goto, break, continue, or return; nor may it be entered with a 
goto.", but this is obviously not the case in the following code. The 
closeResources() method throws an exception which exits the finally 
statement. DMD simply is not allowing code which would handle the exception.

void closeResources() {
	throw new Exception("from throwsException");
}

void main() {
	// open resources
	try {
		// use resources
		
	} catch(Exception e) {
		// handle exception
		
	} finally {
		closeResources();
	}
}


If this exception is really a part of the language, shouldn't the gdc 
compiler give the same behavior? The following code compiles with gdc 
but not dmd.

void closeResources() {
	throw new Exception("from throwsException");
}

void main() {
	// open resources
	try {
		// use resources
		
	} catch(Exception e) {
		// handle exception
		
	} finally {
		try {
			closeResources();
		} catch (Exception e) {
		}
	}
}


Thanks,
   Bradley



More information about the Digitalmars-d-learn mailing list