digitalmars.D - Chaining exceptions
- Andrei Alexandrescu (24/24) Nov 18 2009 Consider:
- Sean Kelly (13/35) Nov 18 2009 The Exception class already has a "next" property, but I've always seen ...
- Andrei Alexandrescu (46/97) Nov 18 2009 Guilty as charged. Haven't tried. I don't have D installed at work, but
- Sean Kelly (4/26) Nov 18 2009 Upon reflection, I'm inclined to agree. This is pretty much a nonexiste...
- Jesse Phillips (16/47) Nov 18 2009 Best as I can tell, the Java compiler doesn't do the chaining
- Andrei Alexandrescu (4/56) Nov 18 2009 Thanks! Question - is there a way to fetch the current Throwable from
- Sean Kelly (2/5) Nov 18 2009 FWIW, I've been thinking about adding this feature for ages. I think th...
- Jesse Phillips (3/7) Nov 19 2009 I'm pretty sure you can't since finally isn't passed an exception. I als...
- BCS (3/13) Nov 20 2009 That stands to reason because in some cases (when things go correctly) t...
- Chris Nicholson-Sauls (17/34) Nov 20 2009 One hopes. Just the same: (hypothetical syntax incoming)
- Andrei Alexandrescu (4/42) Nov 20 2009 I'm not that fancy. An API call getCurrentError returning an Error
- Chris Nicholson-Sauls (5/49) Nov 21 2009 At which point one could argue that the constructor of Throwable or Exce...
- BCS (3/25) Nov 21 2009 IIRC the .NET CLI has some of what you want:
Consider: void fun() { try { throw new Exception("a"); } finally { throw new Exception("b"); } } Currently this function would unceremoniously terminate the program. I think it shouldn't. What should happen is that the "a" exception should be propagated unabated, and the "b" exception should be appended to it. The Exception class should have a property "next" that returns a reference to the next exception thrown (in this case "b"), effectively establishing an arbitrarily long singly-linked list of exceptions. A friend told me that that's what Java does, with the difference that the last exception thrown takes over, so the chain comes reversed. I strongly believe "a" is the main exception and "b" is a contingent exception, so we shouldn't do what Java does. But Java must have some good reason to go the other way. Please chime in with (a) a confirmation/infirmation of Java's mechanism above; (b) links to motivations for Java's approach, (c) any comments about all of the above. Thanks, Andrei
Nov 18 2009
Andrei Alexandrescu Wrote:Consider: void fun() { try { throw new Exception("a"); } finally { throw new Exception("b"); } } Currently this function would unceremoniously terminate the program. I think it shouldn't.Have you tried it? I think the call to terminate() is commented out. It was like this before I ever started mucking with the runtime many moons ago. What I think currently happens is that the exception in the finally clause will replace the one being thrown. That or it's discarded, I really can't remember which. Either way, the current behavior is a bit weird.What should happen is that the "a" exception should be propagated unabated, and the "b" exception should be appended to it. The Exception class should have a property "next" that returns a reference to the next exception thrown (in this case "b"), effectively establishing an arbitrarily long singly-linked list of exceptions.The Exception class already has a "next" property, but I've always seen this as a way to nest exceptions. For example: try { throw new Exception; } catch( Exception e ) { throw new MyException( e ); } So a network API might throw a NetworkException that references a SocketException with more detailed info about the exact problem, etc. The reason I'm unsure about chaining related exceptions as opposed to use chaining as a means of repackaging exceptions is that the catch handler that ultimately executes will be the one that matches the first exception in the chain, not the first that matches any exception in the chain. I haven't thought about this too carefully, but it seems like it might be difficult to write correct code with this model.A friend told me that that's what Java does, with the difference that the last exception thrown takes over, so the chain comes reversed. I strongly believe "a" is the main exception and "b" is a contingent exception, so we shouldn't do what Java does. But Java must have some good reason to go the other way.When a dozen holes appear in a dike, I'm not sure it matters which one you try to plug first :-) Unless there's some way to start with the biggest one, I suppose. Seems like with the suggested model, the correct approach may be to always catch Exception and walk the whole chain to figure out what to do. But that sounds awfully close to C-style error handling.
Nov 18 2009
Sean Kelly wrote:Andrei Alexandrescu Wrote:Guilty as charged. Haven't tried. I don't have D installed at work, but I tried a Java example and indeed it looks like the last exception thrown just takes over. class Test { public static void main(String[] args) { System.out.println("Hello World!"); try { (new Test()).fun(); } catch (Exception e) { System.out.print(e); System.out.print(e.getCause()); System.out.print("\n"); e.printStackTrace(); } } void fun() throws Exception { try { throw new Exception("a"); } finally { throw new Exception("b"); } } } Even more interestingly, calling printStackTrace() does not acknowledge the originating exception. Calling getCause() returns null. So essentially at the catch point I'm not sure there's a way to get to the "a" exception.Consider: void fun() { try { throw new Exception("a"); } finally { throw new Exception("b"); } } Currently this function would unceremoniously terminate the program. I think it shouldn't.Have you tried it? I think the call to terminate() is commented out. It was like this before I ever started mucking with the runtime many moons ago. What I think currently happens is that the exception in the finally clause will replace the one being thrown. That or it's discarded, I really can't remember which. Either way, the current behavior is a bit weird.Well I'm not sure about the metaphor. What I can tell from my code is that exceptions are often contingent one upon another (not parallel and independent). A typical example: writing to a file fails, but then closing it also fails and possibly attempting to remove the partial file off disk also fails. In that case, the important message is that the file couldn't be written to; the rest is aftermath. If the doctor says "You have a liver problem, which causes your nails to have a distinctive shape" you don't mind the nails as much as the liver. There's also the opposite flow, for example an exception in some validation prevents a database update. But I don't think code regularly does essential work in destructors, finally blocks, and scope statements. The essential work is done on the straight path, and the important error is happening on the straight path. In fact, what I just wrote tilted me a bit more in favor of the "master exception + contingent camarilla" model. That model also suggests that most of the time you only need to look at the top exception thrown to figure out the root of the problem; talking to the camarilla is optional. AndreiWhat should happen is that the "a" exception should be propagated unabated, and the "b" exception should be appended to it. The Exception class should have a property "next" that returns a reference to the next exception thrown (in this case "b"), effectively establishing an arbitrarily long singly-linked list of exceptions.The Exception class already has a "next" property, but I've always seen this as a way to nest exceptions. For example: try { throw new Exception; } catch( Exception e ) { throw new MyException( e ); } So a network API might throw a NetworkException that references a SocketException with more detailed info about the exact problem, etc. The reason I'm unsure about chaining related exceptions as opposed to use chaining as a means of repackaging exceptions is that the catch handler that ultimately executes will be the one that matches the first exception in the chain, not the first that matches any exception in the chain. I haven't thought about this too carefully, but it seems like it might be difficult to write correct code with this model.A friend told me that that's what Java does, with the difference that the last exception thrown takes over, so the chain comes reversed. I strongly believe "a" is the main exception and "b" is a contingent exception, so we shouldn't do what Java does. But Java must have some good reason to go the other way.When a dozen holes appear in a dike, I'm not sure it matters which one you try to plug first :-) Unless there's some way to start with the biggest one, I suppose. Seems like with the suggested model, the correct approach may be to always catch Exception and walk the whole chain to figure out what to do. But that sounds awfully close to C-style error handling.
Nov 18 2009
Andrei Alexandrescu Wrote:Sean Kelly wrote:Upon reflection, I'm inclined to agree. This is pretty much a nonexistent case for me anyway (my experience with C++ has me following the "no exceptions from dtors ever" mantra for the most part), so it's difficult to come up with counterexamples. I also like that your chaining method represents a timeline of what happened, with the most likely cause of the whole mess at the head of the list.When a dozen holes appear in a dike, I'm not sure it matters which one you try to plug first :-) Unless there's some way to start with the biggest one, I suppose. Seems like with the suggested model, the correct approach may be to always catch Exception and walk the whole chain to figure out what to do. But that sounds awfully close to C-style error handling.Well I'm not sure about the metaphor. What I can tell from my code is that exceptions are often contingent one upon another (not parallel and independent). A typical example: writing to a file fails, but then closing it also fails and possibly attempting to remove the partial file off disk also fails. In that case, the important message is that the file couldn't be written to; the rest is aftermath. If the doctor says "You have a liver problem, which causes your nails to have a distinctive shape" you don't mind the nails as much as the liver.There's also the opposite flow, for example an exception in some validation prevents a database update. But I don't think code regularly does essential work in destructors, finally blocks, and scope statements. The essential work is done on the straight path, and the important error is happening on the straight path.Yeah, I think you're right.
Nov 18 2009
On Wed, 18 Nov 2009 15:24:11 -0800, Andrei Alexandrescu wrote:Consider: void fun() { try { throw new Exception("a"); } finally { throw new Exception("b"); } } Currently this function would unceremoniously terminate the program. I think it shouldn't. What should happen is that the "a" exception should be propagated unabated, and the "b" exception should be appended to it. The Exception class should have a property "next" that returns a reference to the next exception thrown (in this case "b"), effectively establishing an arbitrarily long singly-linked list of exceptions. A friend told me that that's what Java does, with the difference that the last exception thrown takes over, so the chain comes reversed. I strongly believe "a" is the main exception and "b" is a contingent exception, so we shouldn't do what Java does. But Java must have some good reason to go the other way. Please chime in with (a) a confirmation/infirmation of Java's mechanism above; (b) links to motivations for Java's approach, (c) any comments about all of the above. Thanks, AndreiBest as I can tell, the Java compiler doesn't do the chaining automatically. It is up to the one throwing the exception to make the chain. The exception class just provides a specification that requires all exceptions to support chaining. This explains why it is not the root cause that is at the head of the chain. try { stmt.executeUpdate(sql); } catch (SQLException ex) { throw new EmployeeLookupException( "Query failure",ex); // ex is passed to the constructor of the class } Example from: http://java.sys-con.com/node/36579 http://www.developer.com/tech/article.php/1431531/Chained-Exceptions-in- Java.htm
Nov 18 2009
Jesse Phillips wrote:On Wed, 18 Nov 2009 15:24:11 -0800, Andrei Alexandrescu wrote:Thanks! Question - is there a way to fetch the current Throwable from within a finally clause? AndreiConsider: void fun() { try { throw new Exception("a"); } finally { throw new Exception("b"); } } Currently this function would unceremoniously terminate the program. I think it shouldn't. What should happen is that the "a" exception should be propagated unabated, and the "b" exception should be appended to it. The Exception class should have a property "next" that returns a reference to the next exception thrown (in this case "b"), effectively establishing an arbitrarily long singly-linked list of exceptions. A friend told me that that's what Java does, with the difference that the last exception thrown takes over, so the chain comes reversed. I strongly believe "a" is the main exception and "b" is a contingent exception, so we shouldn't do what Java does. But Java must have some good reason to go the other way. Please chime in with (a) a confirmation/infirmation of Java's mechanism above; (b) links to motivations for Java's approach, (c) any comments about all of the above. Thanks, AndreiBest as I can tell, the Java compiler doesn't do the chaining automatically. It is up to the one throwing the exception to make the chain. The exception class just provides a specification that requires all exceptions to support chaining. This explains why it is not the root cause that is at the head of the chain. try { stmt.executeUpdate(sql); } catch (SQLException ex) { throw new EmployeeLookupException( "Query failure",ex); // ex is passed to the constructor of the class } Example from: http://java.sys-con.com/node/36579 http://www.developer.com/tech/article.php/1431531/Chained-Exceptions-in- Java.htm
Nov 18 2009
Andrei Alexandrescu Wrote:Thanks! Question - is there a way to fetch the current Throwable from within a finally clause?FWIW, I've been thinking about adding this feature for ages. I think there's even a comment to the effect in core.thread somewhere. In short, I think we'd need a thread-local field for storing the current exception.
Nov 18 2009
On Wed, 18 Nov 2009 18:27:47 -0800, Andrei Alexandrescu wrote:Thanks! Question - is there a way to fetch the current Throwable from within a finally clause? AndreiI'm pretty sure you can't since finally isn't passed an exception. I also don't see anything in my quick search.
Nov 19 2009
Hello Jesse,On Wed, 18 Nov 2009 18:27:47 -0800, Andrei Alexandrescu wrote:That stands to reason because in some cases (when things go correctly) there isn't one.Thanks! Question - is there a way to fetch the current Throwable from within a finally clause? AndreiI'm pretty sure you can't since finally isn't passed an exception. I also don't see anything in my quick search.
Nov 20 2009
BCS wrote:Hello Jesse,One hopes. Just the same: (hypothetical syntax incoming) try { // ... } catch ( ExceptionA exa ) { // ... } catch ( ExceptionB exb ) { // ... } finally ( x ) { // ... if ( x ) throw new ExceptionC( x ); } And obviously if finally has no () it doesn't bother with the feature. -- Chris Nicholson-SaulsOn Wed, 18 Nov 2009 18:27:47 -0800, Andrei Alexandrescu wrote:That stands to reason because in some cases (when things go correctly) there isn't one.Thanks! Question - is there a way to fetch the current Throwable from within a finally clause? AndreiI'm pretty sure you can't since finally isn't passed an exception. I also don't see anything in my quick search.
Nov 20 2009
Chris Nicholson-Sauls wrote:BCS wrote:I'm not that fancy. An API call getCurrentError returning an Error object or null is all that's needed. And more general too. AndreiHello Jesse,One hopes. Just the same: (hypothetical syntax incoming) try { // ... } catch ( ExceptionA exa ) { // ... } catch ( ExceptionB exb ) { // ... } finally ( x ) { // ... if ( x ) throw new ExceptionC( x ); } And obviously if finally has no () it doesn't bother with the feature. -- Chris Nicholson-SaulsOn Wed, 18 Nov 2009 18:27:47 -0800, Andrei Alexandrescu wrote:That stands to reason because in some cases (when things go correctly) there isn't one.Thanks! Question - is there a way to fetch the current Throwable from within a finally clause? AndreiI'm pretty sure you can't since finally isn't passed an exception. I also don't see anything in my quick search.
Nov 20 2009
Andrei Alexandrescu wrote:Chris Nicholson-Sauls wrote:At which point one could argue that the constructor of Throwable or Exception could/should call that same API function itself. I'm not strictly opposed to it, mind you, it just bodes ill not to thoroughly examine such auto-magic. -- Chris Nicholson-SaulsBCS wrote:I'm not that fancy. An API call getCurrentError returning an Error object or null is all that's needed. And more general too. AndreiHello Jesse,One hopes. Just the same: (hypothetical syntax incoming) try { // ... } catch ( ExceptionA exa ) { // ... } catch ( ExceptionB exb ) { // ... } finally ( x ) { // ... if ( x ) throw new ExceptionC( x ); } And obviously if finally has no () it doesn't bother with the feature. -- Chris Nicholson-SaulsOn Wed, 18 Nov 2009 18:27:47 -0800, Andrei Alexandrescu wrote:That stands to reason because in some cases (when things go correctly) there isn't one.Thanks! Question - is there a way to fetch the current Throwable from within a finally clause? AndreiI'm pretty sure you can't since finally isn't passed an exception. I also don't see anything in my quick search.
Nov 21 2009
Hello Chris Nicholson-Sauls,BCS wrote:IIRC the .NET CLI has some of what you want: http://stackoverflow.com/questions/533968/c-finally-block-that-only-runs-on-exceptionsThat stands to reason because in some cases (when things go correctly) there isn't one.One hopes. Just the same: (hypothetical syntax incoming) try { // ... } catch ( ExceptionA exa ) { // ... } catch ( ExceptionB exb ) { // ... } finally ( x ) { // ... if ( x ) throw new ExceptionC( x ); } And obviously if finally has no () it doesn't bother with the feature.
Nov 21 2009