Dan,

If it walks like a duck…

You make a great case why Go should have checked exceptions. If anything the Go 
error handling model tries to be checked exceptions - every error should be 
explicitly handled - without any of the conveniences of exceptions - common 
blocks, stack traces and call stack unwinding.  

> On Oct 24, 2022, at 4:52 AM, Harri L <harl...@gmail.com> wrote:
> 
> Hi,
> 
>>> On Friday, October 21, 2022 at 12:14:52 AM UTC+3 dple...@google.com wrote:
>>> 
>>> var x0 float
>>> try {
>>>    x0 = check DoSomeMath(check FetchSomething(), check ComputeSomething())
>>> } handle err {
>>>    log.Info("Unable to estimate initial approximation, defaulting to 1...")
>>>    x0 = 1
>>> }
>>> // code continues and does things with x
>>  
>> I wanted to show what the previous block looks like with the help of the 
>> current Go and some help from the err2-package (I'm the author). The Go 
>> playground.
>> 
>>      calcSomething := func() (x0 int) {
>>         defer err2.Catch(func(err error) {
>>             log.Printf("Unable to estimate initial approximation: %v, 
>> defaulting to 1...", err)
>>             x0 = 1
>>         })
>>         return try.To1(DoSomeMath(try.To1(FetchSomething()), 
>> try.To1(ComputeSomething())))
>>     }
>>     x0 := calcSomething()
>> 
>> Of course, that's not exactly what you are proposing:
>> 
>> This makes it easy to see what error handling will happen at any point 
>> within the function, keeps the control flow linear (so that, unlike 
>> defer()-based recovery, you don't have to skip ahead in the function to get 
>> context before the handler makes sense - the context comes first, followed 
>> by the handling code), and allows code to recover from errors without 
>> aborting an entire function.
>  
> I also wanted to say that the `defer` -based error handling isn't so bad when 
> you are used to the defer keyword already in the Go. 
> 
> Best Regards,
> -Harri
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/1d1e051d-4485-4fe0-a9c8-16afdc5d80e6n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/69E919A4-6F98-49D5-83A4-520D2FCC12F0%40ix.netcom.com.

Reply via email to