如何在catch中打印错误(how to print error in catch)

编程入门 行业动态 更新时间:2024-10-24 14:25:10
如何在catch中打印错误(how to print error in catch) catch let error as LocksmithError{ print(error)// it would print the case of the error. }

但是,如果我这样做

catch LocksmithError.Duplicate{ } catch{ print (LocksmithError) // Obviously I would just print LocksmithError, it won't print the case print (LocksmithError.rawValue) // prints nothing }

我的问题是:使用第二种方法是否有任何我可以实际检索的和错误的价值/案例? 或者,如果我没有在入口点获得正确的值,即捕获量,那么我错过了这样做的机会!

catch let error as LocksmithError{ print(error)// it would print the case of the error. }

However if I do

catch LocksmithError.Duplicate{ } catch{ print (LocksmithError) // Obviously I would just print LocksmithError, it won't print the case print (LocksmithError.rawValue) // prints nothing }

My question is: Using the 2nd approach is there any that I can actually retrieve and the value/case of the error? Or if I don't get the value right at the entry point ie the catch, then I miss the chance of doing it!

最满意答案

catch块是独占的情况,按顺序进行评估。 当比赛成功时,我们停下来

那么,让我们考虑一下这个结构:

catch LocksmithError.Duplicate { // 1 print("duplicate") } catch { // 2 print(error) }

如果我们在1 ,那么范围是LocksmithError.Duplicate 。

如果我们在2 ,那么范围内的是所有其他类型的错误被捕获。 你无法在这里获得LocksmithError.Duplicate ,因为前假设它会被捕获1并且我们不会这里。

现在, 这样做的方式是这样的:

catch let err as LocksmithError { // 1 print(err) } catch { // 2 print(error) }

这可能是你所追求的事情; 它给我们一个值err ,它将错误带入1中的花括号中。 (自动error值仅存在于最终的catch-all catch块。)

The catch blocks are exclusive cases, evaluated in order. When a match succeeds, we stop.

So, let's just think about this structure:

catch LocksmithError.Duplicate { // 1 print("duplicate") } catch { // 2 print(error) }

If we are at 1, then what is in scope is the LocksmithError.Duplicate.

If we are at 2, then what is in scope is every other kind of error that gets caught. There's no way you can get hold of the LocksmithError.Duplicate here, because ex hypothesi it would have been caught in 1 and we wouldn't be here.

Now, the way I would do it is like this:

catch let err as LocksmithError { // 1 print(err) } catch { // 2 print(error) }

That may be the sort of thing you are after; it gives us a value err that carries the error into the curly braces in 1. (The automatic error value exists only the final catch-all catch block.)

更多推荐

本文发布于:2023-08-02 01:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1367997.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   如何在   error   catch   print

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!