January 12, 2011 [phobos] Exception chaining | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | On 12 January 2011 16:00, Sean Kelly <sean at invisibleduck.org> wrote: > I think scope(failure) may need some work though, since it's modeled as a catch block. Yeah, if it throws an exception it will clobber any existing ones, instead of chaining with them. I think it should be implemented as a finally{} which is bypassed in normal execution. > > Sent from my iPhone > > On Jan 12, 2011, at 12:46 AM, Don Clugston <dclugston at googlemail.com> wrote: > >> No, that's unaffected by the scheme. The scheme only affects situations when an exception is thrown from inside a finally clause, when the finally clause is being executed because an exception had been thrown. It doesn't affect catch clauses, because once you're inside the catch, the first exception is no longer in flight. >> >> >> On 12 January 2011 09:27, Max Samukha <maxsamukha at gmail.com> wrote: >>> Will one be able to replace exceptions? A common C# scenario: >>> >>> class SystemException : Exception >>> { >>> ? ? this(string msg, Exception innerEx) { this(msg, innerEx); } >>> } >>> >>> class SubsystemException : Exception >>> { >>> ? ? ?this(string msg) { this(msg); } >>> } >>> >>> void system() >>> { >>> ? ? try >>> ? ? { >>> ? ? ? ? ?subsystem(); >>> ? ? } >>> ? ? catch (SubsystemException ex) >>> ? ? { >>> ? ? ? ? // subsystem exception is replaced with system exception and linked >>> to the latter >>> ? ? ? ? throw new SystemException("a system exception", ex); >>> ? ? } >>> } >>> >>> void subsystem() >>> { >>> ? ? throw new SubsystemException("a subsystem exception"); >>> } >>> >>> void main() >>> { >>> ? ? ? try >>> ? ? ? { >>> ? ? ? ? ? system(); >>> ? ? ? } >>> ? ? ? catch (SystemException ex) >>> ? ? ? { >>> ? ? ? ? ? ?// catch system exceptions and subsystem exceptions are available >>> via innerException property >>> ? ? ? ? ? ?writeln("system: ", ex, ", subsystem: ", ex.innerException); >>> >>> ? ? ? } >>> } >>> >>> As far as I understand, your scheme makes the above problematic. >>> >>> On Wed, Jan 12, 2011 at 1:50 AM, Andrei Alexandrescu <andrei at erdani.com> wrote: >>>> >>>> I don't think that's helpful. It complicates the flow a lot because now understanding how the program acts depends not on the types anymore, but on what happens dynamically. Makes it more difficult, not easier, to write robust code. >>>> >>>> If I throw a FileException, I must catch a FileException with catch(FileException) regardless of what collateral exceptions have happened. >>>> >>>> >>>> Andrei >>>> >>>> On 1/11/11 12:31 PM, Don Clugston wrote: >>>>> >>>>> I've thought about this a bit more. Another simple rule is, that an >>>>> exception chain can be caught if ?and only if every exception in that >>>>> chain can be caught. >>>>> So, for example, >>>>> catch(FileException) will catch multiple file exceptions. >>>>> catch(Exception) will catch any exception (but not Errors). >>>>> catch(Throwable) catches Errors as well. >>>>> >>>>> I went ahead and implemented this. Everythings seems to Just Work. Will check it in shortly. >>>>> >>>>> >>>>> On 11 January 2011 18:30, Andrei Alexandrescu<andrei at erdani.com> ?wrote: >>>>>> >>>>>> Wow, this is incredible news! >>>>>> >>>>>> Thanks Don for working on this. Solid exception handling is a huge >>>>>> selling >>>>>> point for D. >>>>>> >>>>>> Regarding collateral throwables that are not Exception, good point (and >>>>>> I >>>>>> agree that the solution should be simple). TDPL doesn't discuss that >>>>>> issue, >>>>>> but it says that the initially-thrown exception is the "boss" and that >>>>>> everybody follows, so a possible design is to simply make the Throwable >>>>>> part >>>>>> of the chain. >>>>>> >>>>>> I'd want to have chained exceptions still catchable by catch (Exception) >>>>>> because it would be a first to have the contents of the data influence >>>>>> its >>>>>> type. As far as the type system is concerned, catch (Exception) should >>>>>> catch >>>>>> Exceptions, whether or not they have a tail. >>>>>> >>>>>> One possibility would be to move the Throwable to the front of the list. >>>>>> This also has its issues, for example the stack is unwound for a while >>>>>> and >>>>>> then not anymore (a Throwable is allowed to respect fewer rules than an >>>>>> Exception). >>>>>> >>>>>> Ideas please? >>>>>> >>>>>> >>>>>> Andrei >>>>>> >>>>>> On 1/11/11 1:57 AM, Don Clugston wrote: >>>>>>> >>>>>>> I believe I have got TDPL exception chaining working correctly using >>>>>>> Windows Structured Exception Handling. >>>>>>> (This was far from easy!) >>>>>>> Central to making chaining work correctly, is that chaining must only >>>>>>> occur >>>>>>> when a collision occurs (not merely when two exceptions are in flight, >>>>>>> because one may be caught before it has any effect on the other). This >>>>>>> means that multiple chains of exceptions >>>>>>> may be in flight at any given time. >>>>>>> My code works in all nasty corner cases I've tested, including >>>>>>> multi-level collisions, >>>>>>> where two exceptions collide in a function, then collide again with an >>>>>>> even earlier exception chain in a finally block in a different >>>>>>> function. >>>>>>> >>>>>>> So the general scheme appears to work. >>>>>>> But, there's something I'm unclear about. When should chained >>>>>>> exceptions be catchable? >>>>>>> They are very nasty creatures, and you really want to know when they >>>>>>> happen. >>>>>>> Presumably, an AssertError which occurs while processing an >>>>>>> FileException, should not be silently chained >>>>>>> and caught in the FileException. >>>>>>> In fact, should a chain containing an Error be catchable at all? >>>>>>> (If not, it still has to at least be catchable in the catchall handler >>>>>>> that wraps main()). >>>>>>> Many other schemes are possible, but I think it's important that the >>>>>>> rules remain simple. >>>>>>> >>>>>>> One simple solution would be to make chained exceptions only catchable >>>>>>> by catch(Throwable). >>>>>>> _______________________________________________ >>>>>>> phobos mailing list >>>>>>> phobos at puremagic.com >>>>>>> http://lists.puremagic.com/mailman/listinfo/phobos >>>>>> >>>>>> _______________________________________________ >>>>>> phobos mailing list >>>>>> phobos at puremagic.com >>>>>> http://lists.puremagic.com/mailman/listinfo/phobos >>>>>> >>>>> _______________________________________________ >>>>> phobos mailing list >>>>> phobos at puremagic.com >>>>> http://lists.puremagic.com/mailman/listinfo/phobos >>>> >>>> _______________________________________________ >>>> phobos mailing list >>>> phobos at puremagic.com >>>> http://lists.puremagic.com/mailman/listinfo/phobos >>> >>> >>> _______________________________________________ >>> phobos mailing list >>> phobos at puremagic.com >>> http://lists.puremagic.com/mailman/listinfo/phobos >>> >> _______________________________________________ >> phobos mailing list >> phobos at puremagic.com >> http://lists.puremagic.com/mailman/listinfo/phobos > _______________________________________________ > phobos mailing list > phobos at puremagic.com > http://lists.puremagic.com/mailman/listinfo/phobos > |
January 12, 2011 [phobos] Exception chaining | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | On 12 January 2011 16:01, Sean Kelly <sean at invisibleduck.org> wrote: > Two errors could still cause confusion though. Yeah. Though how much of a problem is it? Catching an Error is a nasty action. A nothrow function is permitted to throw an Error because it is non-recoverable, so catching it in order to recover is somehow a bit shady. (Should it be legal to catch an Error in a @safe function?) This whole exception chaining idea is based on the premise that it's the first error which is the important one. I do think that applies even to Errors; I think it's still better than C++'s replacement strategy. Can you think of any other way of doing it? We could say something like, "catching an Error is not something which should be done lightly. In the rare cases where it is necessary, you should _always_ check for secondary Errors which may have occurred while processing the first one." Anyway, I've implemented this second approach. If anyone has ideas for anything better, please speak now, because I want to check it in and move onto something else. (I just want to leave it in a close-enough state, that you can take over from to do the finishing touches). > > Sent from my iPhone > > On Jan 12, 2011, at 12:55 AM, Don Clugston <dclugston at googlemail.com> wrote: > >> On 12 January 2011 09:10, Don Clugston <dclugston at googlemail.com> wrote: >>> How about this rule: >>> --- >>> If all collateral exceptions are derived from Exception, the 'boss' or >>> Master Exception is the first exception thrown. >>> Otherwise, the first Error (or Throwable non-Exception) is the boss. >>> The exception, including all collateral exceptions, will be caught by >>> the first handler who can catch the boss. >>> --- >>> >>> Two issues: >>> * If the boss is a collateral exception, we still need a way to find >>> out what the first exception was. Maybe need to add a 'first' (or >>> 'prev') member to Throwable? >> >> Or, the chain could simply be: >> boss -> exception1 -> exception2 -> BypassThrowable -> exception3. >> >> Where BypassThrowable is a class which acts as a placeholder for the boss. >> >> This would be the simplest interim solution, since it doesn't require >> any code changes elsewhere, and would allow the >> existing error messages to continue to work correctly. >> >> >>> * It is still possible to throw an Object which isn't derived from >>> Throwable. Can we disallow this? >>> (We could throw a "NoThrowableError" if such an object is found to >>> have been thrown (can happen via casting)). This would remove some >>> really nasty corner cases. >>> >>> >>> On 12 January 2011 01:10, Andrei Alexandrescu <andrei at erdani.com> wrote: >>>> On 1/11/11 4:03 PM, Jonathan M Davis wrote: >>>>> >>>>> On Tuesday, January 11, 2011 15:50:52 Andrei Alexandrescu wrote: >>>>>> >>>>>> I don't think that's helpful. It complicates the flow a lot because now understanding how the program acts depends not on the types anymore, but on what happens dynamically. Makes it more difficult, not easier, to write robust code. >>>>>> >>>>>> If I throw a FileException, I must catch a FileException with catch(FileException) regardless of what collateral exceptions have happened. >>>>> >>>>> I agree as long as it's other Exceptions that have been thrown. But >>>>> Errors? >>>>> Aren't they typically supposed to kill your program? >>>>> >>>>> - Jonathan M Davis >>>> >>>> I agree that non-Exception Throwables are an out-of-band method of communication that deserves special attention. >>>> >>>> Don, would it be difficult to make a non-Exception Throwable thrown during unwinding essentially come to the top of the foodchain and save everything in its tail? >>>> >>>> >>>> Andrei >>>> _______________________________________________ >>>> phobos mailing list >>>> phobos at puremagic.com >>>> http://lists.puremagic.com/mailman/listinfo/phobos >>>> >>> >> _______________________________________________ >> phobos mailing list >> phobos at puremagic.com >> http://lists.puremagic.com/mailman/listinfo/phobos > _______________________________________________ > phobos mailing list > phobos at puremagic.com > http://lists.puremagic.com/mailman/listinfo/phobos > |
January 12, 2011 [phobos] Exception chaining | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Sounds fine to me. I think the code is still in place to bypass the chaining functionality when SomeException.classinfo.init is thrown, correct?
On Jan 12, 2011, at 11:56 AM, Don Clugston wrote:
> On 12 January 2011 16:01, Sean Kelly <sean at invisibleduck.org> wrote:
>> Two errors could still cause confusion though.
>
> Yeah. Though how much of a problem is it?
> Catching an Error is a nasty action. A nothrow function is permitted
> to throw an Error because it is non-recoverable, so catching it in
> order to recover is somehow a bit shady. (Should it be legal to catch
> an Error in a @safe function?)
>
> This whole exception chaining idea is based on the premise that it's the first error which is the important one. I do think that applies even to Errors; I think it's still better than C++'s replacement strategy. Can you think of any other way of doing it?
>
> We could say something like, "catching an Error is not something which should be done lightly. In the rare cases where it is necessary, you should _always_ check for secondary Errors which may have occurred while processing the first one."
>
> Anyway, I've implemented this second approach. If anyone has ideas for anything better, please speak now, because I want to check it in and move onto something else. (I just want to leave it in a close-enough state, that you can take over from to do the finishing touches).
>
>
>>
>> Sent from my iPhone
>>
>> On Jan 12, 2011, at 12:55 AM, Don Clugston <dclugston at googlemail.com> wrote:
>>
>>> On 12 January 2011 09:10, Don Clugston <dclugston at googlemail.com> wrote:
>>>> How about this rule:
>>>> ---
>>>> If all collateral exceptions are derived from Exception, the 'boss' or
>>>> Master Exception is the first exception thrown.
>>>> Otherwise, the first Error (or Throwable non-Exception) is the boss.
>>>> The exception, including all collateral exceptions, will be caught by
>>>> the first handler who can catch the boss.
>>>> ---
>>>>
>>>> Two issues:
>>>> * If the boss is a collateral exception, we still need a way to find
>>>> out what the first exception was. Maybe need to add a 'first' (or
>>>> 'prev') member to Throwable?
>>>
>>> Or, the chain could simply be:
>>> boss -> exception1 -> exception2 -> BypassThrowable -> exception3.
>>>
>>> Where BypassThrowable is a class which acts as a placeholder for the boss.
>>>
>>> This would be the simplest interim solution, since it doesn't require
>>> any code changes elsewhere, and would allow the
>>> existing error messages to continue to work correctly.
>>>
>>>
>>>> * It is still possible to throw an Object which isn't derived from
>>>> Throwable. Can we disallow this?
>>>> (We could throw a "NoThrowableError" if such an object is found to
>>>> have been thrown (can happen via casting)). This would remove some
>>>> really nasty corner cases.
>>>>
>>>>
>>>> On 12 January 2011 01:10, Andrei Alexandrescu <andrei at erdani.com> wrote:
>>>>> On 1/11/11 4:03 PM, Jonathan M Davis wrote:
>>>>>>
>>>>>> On Tuesday, January 11, 2011 15:50:52 Andrei Alexandrescu wrote:
>>>>>>>
>>>>>>> I don't think that's helpful. It complicates the flow a lot because now understanding how the program acts depends not on the types anymore, but on what happens dynamically. Makes it more difficult, not easier, to write robust code.
>>>>>>>
>>>>>>> If I throw a FileException, I must catch a FileException with catch(FileException) regardless of what collateral exceptions have happened.
>>>>>>
>>>>>> I agree as long as it's other Exceptions that have been thrown. But
>>>>>> Errors?
>>>>>> Aren't they typically supposed to kill your program?
>>>>>>
>>>>>> - Jonathan M Davis
>>>>>
>>>>> I agree that non-Exception Throwables are an out-of-band method of communication that deserves special attention.
>>>>>
>>>>> Don, would it be difficult to make a non-Exception Throwable thrown during unwinding essentially come to the top of the foodchain and save everything in its tail?
>>>>>
>>>>>
>>>>> Andrei
>>>>> _______________________________________________
>>>>> phobos mailing list
>>>>> phobos at puremagic.com
>>>>> http://lists.puremagic.com/mailman/listinfo/phobos
>>>>>
>>>>
>>> _______________________________________________
>>> phobos mailing list
>>> phobos at puremagic.com
>>> http://lists.puremagic.com/mailman/listinfo/phobos
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
>>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
|
January 13, 2011 [phobos] Exception chaining | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Thanks for continuing to think of the best design for this, Don. On 1/12/11 12:10 AM, Don Clugston wrote: > How about this rule: > --- > If all collateral exceptions are derived from Exception, the 'boss' or > Master Exception is the first exception thrown. > Otherwise, the first Error (or Throwable non-Exception) is the boss. > The exception, including all collateral exceptions, will be caught by > the first handler who can catch the boss. > --- This is very sensible. > Two issues: > * If the boss is a collateral exception, we still need a way to find > out what the first exception was. Maybe need to add a 'first' (or > 'prev') member to Throwable? Let's save that option for the future. > * It is still possible to throw an Object which isn't derived from Throwable. Can we disallow this? Yes please! Also, as subsequently discussed, I, too, think it's reasonable to lose state if a non-Exception Throwable gets thrown. Andrei |
January 13, 2011 [phobos] Exception chaining | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | On 1/12/11 11:56 AM, Don Clugston wrote: > On 12 January 2011 16:01, Sean Kelly<sean at invisibleduck.org> wrote: >> Two errors could still cause confusion though. > > Yeah. Though how much of a problem is it? > Catching an Error is a nasty action. A nothrow function is permitted > to throw an Error because it is non-recoverable, so catching it in > order to recover is somehow a bit shady. (Should it be legal to catch > an Error in a @safe function?) I think @safe means memory-safe so probably it could. Catching Error means you continue with broken assumptions about the program, but the memory safety guarantees are preserved. > This whole exception chaining idea is based on the premise that it's the first error which is the important one. I do think that applies even to Errors; I think it's still better than C++'s replacement strategy. Can you think of any other way of doing it? I agree Error is an out-of-band signal that trumps over the nice chain. > We could say something like, "catching an Error is not something which should be done lightly. In the rare cases where it is necessary, you should _always_ check for secondary Errors which may have occurred while processing the first one." I'd even be okay with throwing an Error replaces the whole chain. Error is a worst-case scenario in which you're allowed to lose state and reduce guarantees. Andrei |
Copyright © 1999-2021 by the D Language Foundation