Palatable Exception Handling V2ex 1: Let parents handle itrows = db.RunQuery(query)print("No exception happened")ex 2: I'll handle a certain exceptionrows, e = db.__RunQuery(query)handle(e) {if e != nil and db.IsDuplicateError(e) {print("eh, no big deal")e.handled = True}}print("No exception other than Duplicate happened")ex 3: I'll handle / swallow all exceptionsrows, e = db.__RunQuery(query)handle(e) {if e != nil {print("we had an oopsies")e.handled = True}}print("I'm indefatigable, hahaha!")
Q: What does the leading __ do?A: It makes any function return the exception instead of raise it.Q: What does the handle() { } do?A: It makes it hard to accidentally swallow something you didn't mean to. You have to explicitly say e.handled = True (i.e. hit the snooze button). Otherwise, a non-nil exception that wasn't handled gets re-raised when the block ends.Q: But what's stopping you from not using a handle() block at all, and checking the exception badly by hand?A: Nothing. However, static analysis could warn on this (exception object never passed to any handle() block). Could be a style rule for large projects.Q: But nobody needs exceptions!A: I have no intention of taking my nice python code base and doubling/tripling its size due to menial checks for all database, network, and filesystem calls (which the other devs will assuredly screw up). You guys are out to lunch.- Karl
Palatable Exception Handling V2ex 1: Let parents handle itrows = db.RunQuery(query)print("No exception happened")ex 2: I'll handle a certain exceptionrows, e = db.__RunQuery(query)handle(e) {if e != nil and db.IsDuplicateError(e) {print("eh, no big deal")e.handled = True}}print("No exception other than Duplicate happened")
ex 3: I'll handle / swallow all exceptionsrows, e = db.__RunQuery(query)handle(e) {if e != nil {print("we had an oopsies")e.handled = True}}print("I'm indefatigable, hahaha!")Q: What does the leading __ do?A: It makes any function return the exception instead of raise it.
Q: What does the handle() { } do?A: It makes it hard to accidentally swallow something you didn't mean to. You have to explicitly say e.handled = True (i.e. hit the snooze button). Otherwise, a non-nil exception that wasn't handled gets re-raised when the block ends.
Q: But what's stopping you from not using a handle() block at all, and checking the exception badly by hand?A: Nothing. However, static analysis could warn on this (exception object never passed to any handle() block). Could be a style rule for large projects.
Q: But nobody needs exceptions!A: I have no intention of taking my nice python code base and doubling/tripling its size due to menial checks for all database, network, and filesystem calls (which the other devs will assuredly screw up). You guys are out to lunch.- Karl
Has this mail? been separated from another thead? what does that code do? what's handle?
I'm definitely not a fan of Go's error handing style but I don't like
this any better because the error handling is still inline with the
code.
One thing that try/catch gives me is proper separation of concerns,
exception handling is separated from application logic.
I would like an error handling style that doesn't force me to pollute
my application code with a boatload of error handling trivia.
On Jun 29, 4:43 pm, "Thomas Bushnell, BSG" <tbushn...@google.com>
wrote:
> In my experience, the view that these are "separate concerns" isPoor logic...
> responsible for some of the worst engineering.
>
> When error handling is trivia, it is usually treated as something to be
> done later, and things which are done later are usually simply not done.
>
A developer that will not bother using try/catch to handle errors
after a *block* of code is certainly not going to bother with writing
error handling after every frickin line of code.
And...
...try/catch results in code that more readable and maintainable.
...exception handling results in faster running code than error
checking.
On Sat, Jun 30, 2012 at 1:10 AM, Patrick Mylund Nielsen
<pat...@patrickmylund.com> wrote:
> Couldn't disagree more. It's not that people are actively trying to ignore
> errors, it's that they don't know how or where to handle them. There is
> simply no way to write an idiomatic try/except block for something in a
> programming language like Python without first experiencing the error, or
> without looking at the documentation for the package (or its source code if
> it's poorly documented.) try: except: (all exceptions) is frowned upon.
In python (and other languages with similar exceptions mechanisms), is
even worse than that, often it is not documented in the API which
exceptions a call might throw.
Java's "checked" exceptions are much maligned, and for good reasons,
but at least they ensure that you know what exceptions to expect.
People like exceptions because the truth is that 99% of the time they
ignore them, and simply pretend they can never happen or that somebody
else up the stack will take care of them.
Python libraries have to document not just all the exceptions they
might throw, but all the exceptions any function they call might
throw.
Nobody documents this properly, much less people using the API handle
all the exceptions properly.
> Go makes this a no-brainer. In the majority of cases, all you need to know
> is the function's type signature.
Go is not perfect, but its error handling is clearly superior to
exceptions in every possible way: explicit, clean, concise, is easy to
follow and to know what might or might not happen.
In languages with exceptions, if you see a function call, you have
really no idea what is going to happen, in Go you know exactly what
can and can not happen.
You should never catch oom in java. Att that point the behavior of the jvm is undefined. It often allows you too continue but it is just luck. Next oom may very well happen during a jvm specific allocation in which case it dies the hard way.
Java is no different than Go in this regard.
/Henrik
You should never catch oom in java. Att that point the behavior of the jvm is undefined. It often allows you too continue but it is just luck. Next oom may very well happen during a jvm specific allocation in which case it dies the hard way.
Implementations may die hard but that is a bug. The language spec specifies that the JVM has to raise an InternalError when any operation (after an OOM or not) would lead to violation of a language level invariant.
Clearly some people like exceptions and try/catch blocks. Other people do not. No use trying to convince one side of the other's view point.Go is a language designed to not have exceptions. There are many programming languages to choose from. If you want exceptions then Go is not the programming language for you. Within your own code, if you want, you can use panic/recover, but you should never expect other code to recover your panic.-Paul
> If not, then we're back where we started: a Java program is sometimesJava's OutOfMemoryError is only for the failure of the JVM to satisfy
> notified about out-of-memory conditions, and sometimes not, all on compliant
> implementations.
the "new" bytecode instruction as required by a particular JVM
light-weight thread.
Can you give an example where the spec's TCB assumptions hold?> Sure, but I want to stress that there are cases where it will be unable to
> satisfy that instruction and the result is that the program is simply
> immediately terminated.
We believe that coupling exceptions to a control structure, as in the try-catch-finally idiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.
Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. A canonical error type, coupled with Go's other features, makes error handling pleasant but quite different from that in other languages.
Go also has a couple of built-in functions to signal and recover from truly exceptional conditions. The recovery mechanism is executed only as part of a function's state being torn down after an error, which is sufficient to handle catastrophe but requires no extra control structures and, when used well, can result in clean error-handling code.
See the Defer, Panic, and Recover article for details.
If you want exceptions (actually, try/catch/finally) in Go, please read the Go faq.Why does Go not have exceptions?
We believe that coupling exceptions to a control structure, as in the
try-catch-finallyidiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. A canonical error type, coupled with Go's other features, makes error handling pleasant but quite different from that in other languages.
Go also has a couple of built-in functions to signal and recover from truly exceptional conditions. The recovery mechanism is executed only as part of a function's state being torn down after an error, which is sufficient to handle catastrophe but requires no extra control structures and, when used well, can result in clean error-handling code.
See the Defer, Panic, and Recover article for details.
You may strongly disagree with the Go authors. I strongly agree with them. If you are uncomfortable writing code without try/catch/finally then Go is not the language for you. There is no point in arguing for something the Go authors have pretty clearly stated they are opposed to. You are free to use a different language or create your own.
I'm sorry, you must not have actually read my proposal because it doesn't use anything like try/catch/finally.
In python though, I can use either method of signaling errors. Sometimes exceptions, sometimes return codes. Either way in python is fairly easy. It depends on the project and even the module in the project. Error handling is never one size fits all.
On Tuesday, July 3, 2012 10:00:10 AM UTC-5, Paul Borman wrote:If you want exceptions (actually, try/catch/finally) in Go, please read the Go faq.Why does Go not have exceptions?
We believe that coupling exceptions to a control structure, as in the
try-catch-finallyidiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. A canonical error type, coupled with Go's other features, makes error handling pleasant but quite different from that in other languages.
Go also has a couple of built-in functions to signal and recover from truly exceptional conditions. The recovery mechanism is executed only as part of a function's state being torn down after an error, which is sufficient to handle catastrophe but requires no extra control structures and, when used well, can result in clean error-handling code.
See the Defer, Panic, and Recover article for details.
You may strongly disagree with the Go authors. I strongly agree with them. If you are uncomfortable writing code without try/catch/finally then Go is not the language for you. There is no point in arguing for something the Go authors have pretty clearly stated they are opposed to. You are free to use a different language or create your own.I'm sorry, you must not have actually read my proposal because it doesn't use anything like try/catch/finally.What I have also read on the GO site is incredibly buggy code like:* Most people using fmt.Println are buggy and don't check errors* Everyone doing defer file.Close() is buggy since that doesn't check errors
Go is not perfect, but its error handling is clearly superior to
exceptions in every possible way: explicit, clean, concise, is easy to
follow and to know what might or might not happen.
Is nobody going to step up to bat and defend defer file.Close() as NOT being buggy? Writing a file over NFS with quotas on can make close(2) fail, in the real world. So can closing it twice. Now grep the GO library source code for defer and be afraid. If I wanted a language that hid bugs, I'd use perl.
On Jul 6, 2012 3:27 PM, "Karlito" <karl.p...@gmail.com> wrote:
>
>
>
> On Tuesday, July 3, 2012 10:15:07 AM UTC-5, Karlito wrote:
>>
>>
>> On Tuesday, July 3, 2012 10:00:10 AM UTC-5, Paul Borman wrote:
>>>
>>> If you want exceptions (actually, try/catch/finally) in Go, please read the Go faq.
>>> Why does Go not have exceptions?
>>>
>>> We believe that coupling exceptions to a control structure, as in the try-catch-finally idiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.
>>>
>>> Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. A canonical error type, coupled with Go's other features, makes error handling pleasant but quite different from that in other languages.
>>>
>>> Go also has a couple of built-in functions to signal and recover from truly exceptional conditions. The recovery mechanism is executed only as part of a function's state being torn down after an error, which is sufficient to handle catastrophe but requires no extra control structures and, when used well, can result in clean error-handling code.
>>>
>>> See the Defer, Panic, and Recover article for details.
>>>
>>> You may strongly disagree with the Go authors. I strongly agree with them. If you are uncomfortable writing code without try/catch/finally then Go is not the language for you. There is no point in arguing for something the Go authors have pretty clearly stated they are opposed to. You are free to use a different language or create your own.
>>
>>
>> I'm sorry, you must not have actually read my proposal because it doesn't use anything like try/catch/finally.
>>
>> What I have also read on the GO site is incredibly buggy code like:
>> * Most people using fmt.Println are buggy and don't check errors
>> * Everyone doing defer file.Close() is buggy since that doesn't check errors
>
>
> Is nobody going to step up to bat and defend defer file.Close() as NOT being buggy? Writing a file over NFS with quotas on can make close(2) fail, in the real world. So can closing it twice. Now grep the GO library source code for defer and be afraid. If I wanted a language that hid bugs, I'd use perl.
When I particularly care about checking the error returned by Close, I sometimes write a function like this:
func checkClose(c io.Closer, err *error) {
cerr := c.Close()
if *err == nil {
*err = cerr
}
}
Then I can write
defer checkClose(f, &err)
and not worry that the close error might be ignored.
A rule of good design is to design for the most common usage, while
making it possible to support atypical usage.
This a good rule whether designing a GUI, an API, or a language.
You admitted that the most common usage was less verbose when using
exceptions, and then you went ahead and justified Go's approach using
a very atypical example.
You just turned good design on its head.
A rule of good design is to design for the most common usage, while
making it possible to support atypical usage.
This a good rule whether designing a GUI, an API, or a language.
You admitted that the most common usage was less verbose when using
exceptions, and then you went ahead and justified Go's approach using
a very atypical example.
You just turned good design on its head.