Похожие чаты

Why not just throw and error and i catch it?

15 ответов

20 просмотров

That's how mistakes are made when handling errors

so this... try { f := OpenFile() try { f.Write("foobar") } catch { log.Error("write failed") } } catch { log.Error("open failed") } is easier to you than this? f, err := OpenFile() if err != nil { log.Error("open failed") return } if _, err := f.Write(); err != nil { log.Error("write failed") }

Ghazwan-Aliesh Автор вопроса
Codexetreme
That's how mistakes are made when handling errors

Maybe but if we agree on some interface for the thrown error (error code or something) we can have much less code doing more. I get it that go make it easier to recognize the origin of the error that way but it does affect amount of code I am writing as a developer.

Codexetreme
This is not a good example imo

it's not, sure, but still, try-catch is a very ugly construct IMHO

Ghazwan-Aliesh Автор вопроса
Roman Sharkov
so this... try { f := OpenFile() try { f....

The example u gave for traditional approach is kinda unfair imo. if all the functions that might throw and error followed a similar pattern in the error they throw, I will have one try catch.

Ghazwan Aliesh
The example u gave for traditional approach is kin...

how do you determine which Write call failed without having separate try-catch blocks?

Ghazwan-Aliesh Автор вопроса
Codexetreme
And precisely why try catch is dangerous

yeah maybe go is less evil that way now that i think about it. i am just not used to 20 lines functions i guess 😝

Ghazwan-Aliesh Автор вопроса
Roman Sharkov
how do you determine which Write call failed witho...

Well in most cases I won't care as long as the error thrown provides enough data about itself. let's say a register user function that calls: validation method, database method. if both method will throw the error with a message and code in it, I will just want to return it. Idc where it failed.

Ghazwan Aliesh
Well in most cases I won't care as long as the err...

what if they don't throw any codes? (happens a lot actually)

Ghazwan-Aliesh Автор вопроса
Roman Sharkov
what if they don't throw any codes? (happens a lot...

then the methods are badly implemented, which i realize is what go is trying to protect u from doing.

Roman Sharkov
so this... try { f := OpenFile() try { f....

If you use try catch as if err.. are terrible, if you use in smarter way are very elegant. IMO in several situation go is verbose in error handling, some piece of code I wrote there is more error handling boilerplate than code logic. With try catch, used in the proper way (not as in this example) the whole solution could be more readable and compact. Nevertheless the go error handling has other advantages

Похожие вопросы

Обсуждают сегодня

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
15
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
any reference of this implementation?
BitBuddha
29
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
&"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.42.34433\bin\Hostx64\x64\dumpbin.exe" /EXPORTS C:\Users\Yeet\Tauon\vcpkg\installed\x64-window...
Martin Rys
6
Карта сайта