Похожие чаты

I’m becoming familiar with errors, exits, panic and recovery. Question:

What are the cases where you would prefer a panic over an error? (and potentially an associated recovery?)

As I understand (I could be wrong): A well handled error is better than a panic. Therefore it would be good to hear about some use cases.

7 ответов

15 просмотров

None I guess, right now I can't think of any case where I'd prefer panics. Since panics are errors you weren't able to handle, like using a field of a nil struct, or an slice with invalid index. Generally these are what I face and what the both have in common is that the compiler cannot pre-detect such errors so it panics when they occur.

Justin-K Автор вопроса

None I guess, right now I can't think of any case ...

Thanks, that makes sense. Do you professionally always have to recover those unexpected panics, Or would you allow them to crash.

Here’s a good article/blog post about that topic: https://eli.thegreenplace.net/2018/on-the-uses-and-misuses-of-panics-in-go/ Rob's quote is also quite good there.

Justin K
Thanks, that makes sense. Do you professionally al...

Well I barely recover from panics, I fix the cause instead. The reason is that I find it challenging, guess for example your app panics in the middle of two database queries, if you recover there and recall the function you might have duplicate data in the database. You should handle that separately. Now assume you have a large user base with more than a thousand active users, if you recover from panic and some operations get to complete to some point before the panic, a disaster might happen. You can have a recover from panic but only to show users that an error with system was found and they have to wait for an update. As well as reporting somewhere about the panic and stack trace of the cause.

Panics should rather not be recovered. A goroutine panics when it doesn't know what to do, hence it "panics"

https://github.com/uber-go/guide/blob/master/style.md#dont-panic This may help you as well

Justin-K Автор вопроса
woto 「 噂 」
https://github.com/uber-go/guide/blob/master/style...

I am just now catching up with the discussion from last week. Thank you for your replies: @Falling_inside_The_Black @Romshark @ali_error @mxrkw

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

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

Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
32
Хотел бы спросить у знающих, правильную ли я выбрал книгу для начала изучения ассемблера Юрова В.И ? Или есть более лучшие книги для начала обучения?
Botsman
25
Добрый день, не подскажите, если в OC-V3 поменять страндартную директорию /storage/ на /storage2/ - не будет сильно много проблем ?
Max Dubovsky
32
Книга Юрова В.И пойдёт для обучения?
Botsman
24
Conversation at a festival with a non-crpto person (not a normie by any stretch, though): * person: tell me about crypto, me: ok, the original idea is p2p sound money for the...
molecular#123 🐓
25
$params = [ 'formid' => 'feedbackForm', 'formTpl' => '@CODE: <form class="form-validate" data-id="ajax_form"> <fieldset class="margin-bottom-md"> ...
Pathologic
1
I bought some PHA tokens on Kucoin, which is the best wallet to store and easy to use?
AMR
19
Can you believe it? A whole new gaming ecosystem powered by FunToken! 😲
Josh
13
'frakturBold' => ['𝖆', '𝖇', '𝖈', '𝖉', '𝖊', '𝖋', '𝖌', '𝖍', '𝖎', '𝖏', '𝖐', '𝖑', '𝖒', '𝖓', '𝖔', '𝖕', '𝖖', '𝖗', '𝖘', '𝖙', '𝖚', '𝖛', '𝖜', '𝖝', '𝖞', '𝖟', '𝕬', '𝕭', '𝕮', '𝕯'...
Roma
4
Подскажите пожалуйста, а я могу вот такую штуку использовать? rpc, только реализованное в реббите https://www.rabbitmq.com/tutorials/tutorial-six-php ( или https://habr.com/ru...
Artyom
11
Карта сайта