Should I use a panic or a return error?

Go provides two ways to handle errors, but I'm not sure which one to use.

Assuming I'm implementing a classic ForEach function that takes a slice or map as an argument. To check if the iterability went through, I could do:

 func ForEach(iterable interface{}, f interface{}) { if isNotIterable(iterable) { panic("Should pass in a slice or map!") } } 

or

 func ForEach(iterable interface{}, f interface{}) error { if isNotIterable(iterable) { return fmt.Errorf("Should pass in a slice or map!") } } 

I saw some discussions that said panic() should be avoided, but people also say that if a program cannot recover from an error, you should panic() .

Which should i use? And what is the main principle for choosing the right one?

+18
source share
7 answers

From Dave Cheney :

panics are always fatal to your program. When brewing, you never assume that your caller might solve the problem. Consequently, panic used only in exceptional circumstances when it is not possible for your code or anyone integrating your code to continue.

You must assume that the panic will be immediately fatal, for the entire program, or at least for the current version of goroutine. Ask yourself, "when will this happen if the application crashes immediately?" If so, use panic; otherwise use an error.

+29
source

Use panic .

Because your use case is to catch that your API is not being used as intended . This should never happen at run time if the program calls your API correctly.

In fact, any program that calls your API with the correct arguments will behave the same if the test is removed. The test may fail early with an error message useful to the programmer who made the error. Ideally, panic can be reached once during development when testsuite is run, and the programmer will correct the call even before committing bad code, and this misuse will never reach production.

See also this answer to the question. Is checking function parameters using errors a good example in Go ?.

+4
source

Panic usually means that something unexpectedly happened wrong. It is mainly used for fast failure with errors that should not occur during normal operation, or that we are ready to process gracefully. Therefore, in this case, just return an error, you do not want your program to panic.

0
source

If any mandatory requirement is not provided or not when starting the service (for example, connecting to the database, some necessary configuration of the service), you should use panic.

There should be a return error for any user response or server-side error.

0
source

Ask yourself these questions:

  • Do you expect an exception to occur regardless of how well you code your application? Do you think this should be useful so that the user is aware of this condition as part of the normal use of your application? Treat this as an error, because it concerns the application as working normally.
  • Should this exception not happen if you code properly (and to some extent defensively)? (example: dividing by zero or accessing an array element beyond the bounds) Is your application completely ignorant of this error? Panic.
  • Do you have an API and want users to use it appropriately? Panic. Your API is rarely restored if used incorrectly.
0
source

Do not use panic for normal error handling. Use an error and multiple return values. See https://golang.org/doc/effective_go.html#errors .

-2
source

I prefer to use panic often, which makes it easier to write and debug code logic. See two links below:

-2
source

Source: https://habr.com/ru/post/1268780/


All Articles