如果发生错误并且您的 Golang 应用程序未处理该错误,将会发生什么?

huangapple go评论99阅读模式
英文:

What would happen if an error happened and my golang app didn't handle it?

问题

我目前正在使用gormgin框架。我想知道如果发生错误而我的应用程序没有处理它会发生什么?

例如:

if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil {
    // 错误处理...
}

在上面的示例中,错误被处理了。

if db.Model(&user).Related(&credit_card).RecordNotFound() {
    // 没有找到信用卡的错误处理
}

在上面的下一个示例中,只处理了RecordNotFound()错误,但如果抛出了其他错误会怎么样?会发生什么?

我的应用程序会自动响应500服务器内部错误,并且应用程序会继续正常运行吗?

英文:

I'm currently using gorm and gin framework. I wonder what would happen if an error happened and my app didn't handle it?

Example:

if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil {
    // error handling...
}

In the above example, the error is being handled.

if db.Model(&user).Related(&credit_card).RecordNotFound() {
    // no credit card found error handling
}

In the next example above, only the RecordNotFound() error is being handled, but what if it throws a different error? what will happen?

Will my app automatically respond with a 500 server internal error and will the app keep on running properly?

答案1

得分: 2

在上面的示例中,只处理了RecordNotFound()错误,但如果抛出了其他错误会怎么样呢?

如果你不捕获错误,代码将会继续执行。错误并不是一个特殊类型,它只是一个简单的结构体。

err := FunctionThatReturnsError()
if err == myError.RecordNotFound() { // 如果err不是RecordNotFound,它就不会进入if语句
    // 做一些处理
}

// 继续执行代码

我的应用会自动响应500服务器内部错误吗?应用程序会继续正常运行吗?

如果go routine没有发生panic或者你返回了一个响应,那么就不会有响应。如果你想处理错误,你可以这样做:

err := FunctionThatReturnsError()
if err == myError.RecordNotFound() {
    panic("RecordNotFound")
}

或者

err := FunctionThatReturnsError()
if err == myError.RecordNotFound() {
    c.JSON(500, "Record not found")
}

我不推荐使用panic方法。如果你好奇的话,可以搜索一下为什么。

英文:

> In the next example above, only the RecordNotFound() error is being handled, but what if it throws a different error?

If you won't catch the error it will continue on the code. Error is not a special type it's a simple struct

err := FunctionThatReturnsError()  
if err == myError.RecordNotFound() { // if err is not RecordNotFound then it won't enter the if simple as that. 
    // Do something.  
}  

// continue code.  

> Will my app automatically respond with a 500 server internal error and will the app keep on running properly?

There will be no response if the go routine doesn't panic or you return a response. If you want to handle it you can do:

err := FunctionThatReturnsError()  
if err == myError.RecordNotFound() {
      panic("RecordNotFound")
} 

or

err := FunctionThatReturnsError()  
if err == myError.RecordNotFound() {
      c.JSON(500, "Record not found"}
} 

I don't recommend the panic method. If you're curious google why.

答案2

得分: 2

Go语言没有异常处理机制。与其捕获异常,你可以通过函数的返回值获取错误信息。因此,在幕后并没有抛出异常或其他特殊操作,只是一个返回错误值的函数,就像其他任何返回值一样,你可以忽略它。

不过,我不建议忽略错误。如果你对如何处理错误感到懒惰或困惑,只需将其记录下来:

log.Error(err)

你永远不知道你忽略的错误是否导致了那些你发誓不是来自你自己代码的神秘错误。

英文:

Go doesn't have exceptions. Instead of catching exceptions, you get errors via return values from functions. So there's no throwing or anything special going on behind the scenes, just a function that returns an error value, and like any other return value - you can discard it.

I wouldn't recommend discarding errors though. If you feel lazy or lost about what to do with an error - just log it:

log.Error(err)

You never know if an error you discarded is causing this mysterious bug you can swear is coming from anywhere but your own code.

答案3

得分: 1

我想知道如果发生错误而我的应用程序没有处理它会发生什么?

那么应用程序的状态就是未定义的。如果你不检查错误值,你的应用程序将使用未定义的值(对于指针可能是nil,对于数值可能是“零”),或者假设发生了副作用,但实际上可能没有发生。

假设你有一个带有签名 func CreateStruct() (T, err) 的函数,并像这样调用它 t, _ := CreateStruct()(没有检查错误),你不应该期望 t 变量有一个正确的值。
如果你有一个像 func Update() err 这样的函数,并且在调用时没有进行错误检查,那么你就无法知道是否执行了更新操作。

当然,一切都取决于API和实现。但你明白我的意思。

但是如果它抛出了不同的错误会怎么样?

这是不可能的。Go语言中没有抛出错误的机制。错误只能作为普通值返回。


你绝对不能懒惰地处理错误。这是编程中非常重要的一部分,而Go语言使其更容易实现。

英文:

> I wonder what would happen if an error happened and my app didn't handle it?

Then the state of the app is undefined. If you don't check error values your app will be using values which are undefined (probably nil for pointers and "zeros" for values) or assuming that side effect occurred but it might not.

Let's say you have a function with signature func CreateStruct() (T, err)
and call it like that t, _ := CreateStruct() (not checking for error) you should not expect t variable to have a proper value set.
If you have function like func Update() err and you call it without error checking then you can't know whether update was performed or not.

Of course everything depends on API and implementation. But you get the idea.

> but what if it throws a different error?

It's impossible. There is not throwing error mechanism in Go. Error can only be returned as a normal value.


You should never be lazy with handling errors. It's very important part of programming and Go makes it easier to realize.

huangapple
  • 本文由 发表于 2016年1月1日 17:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/34554999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定