Comparing Exceptions and Errors

Steven Schveighoffer schveiguy at gmail.com
Sat Jun 4 01:17:28 UTC 2022


On 6/3/22 8:44 PM, Ali Çehreli wrote:
> One feedback I have is about non-main threads. Although everybody agrees 
> that Errors should not be caught, the main thread does so and prints the 
> useful output that you show in the article.

Yes, the one exception to the no-catch error rule is to print/log 
diagnostic information. But you should never *continue executing* after 
that.

> If the main thread is allowed to do that, we should do as much (or less) 
> in non-main threads as well. Otherwise, threads disappear without a trace.

If a thread does not catch an error and end the program, that's a defect 
in druntime I think. If it tries to rethrow the exception in the main 
thread (oh, man I have to check... Yeah, this is what it does), then 
it's entirely possible that the main thread will never even get to the 
`Error`!

> If we are paranoid and want to do as little as possible, at least we 
> should attempt to copy a string literal to stderr. Something like 
> "Thread exiting with Error." And exit(1) right after that.

Yes, that is what it should do.

> But all of that is wishful programming because if the program is in an 
> illegal state, should we attempt to report it? If we should not, 
> shouldn't the main thread should not either? (Sorry, non-native Inglish 
> speaker here. :p)

It's useful to do *something*, so it's not just an empty log file with 
no idea where the error occurred.

I've thought in the past that throwing an error really should not throw, 
but log the error (including the call stack), and then exit without even 
attempting to unwind the stack. But code at least expects an attempt to 
throw the Error up the stack, so code that is expecting to catch it 
would break.

-Steve


More information about the Digitalmars-d-learn mailing list