Похожие чаты

Why not just throw and error and i catch it?

15 ответов

22 просмотра

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

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

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

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта