是等同于检查异常吗? [英] Is Either the equivalent to checked exceptions?

查看:107
本文介绍了是等同于检查异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Scala开始阅读或者我自然地将新概念与我所知道的东西(在这种情况下来自Java)进行比较。与被检查的异常的概念有什么不同吗??






  • 在方法中显式注释失败的可能性( throws )或返回或者

  • 程序员可以直接处理错误情况,或者直接处理错误情况(再次返回或者

  • 有一种方法可以告诉调用者有关错误的原因



我想用一个理解或者来编写代码,因为不会有类似于检查异常的错误。



我不知道我是否是唯一一个有问题的初学者,看到这种差异。
$ b $ p

谢谢

解决方案

不仅可用于例外。例如,如果您要让用户为您输入输入或指定包含该输入的文件,则可以将其表示为或者[字符串,文件] 。 p>

常常用于异常处理。 或者和检查的异常之间的主要区别在于,使用或者的控制流始终是显式的。编译器真的不会让你忘记你正在处理或者;它不会收集来自多个地方的 s,而您不知道它,返回的所有内容必须是或者等等。正因为如此,你可以使用或者而不是当某些特别的东西会出错时,而是作为控制程序执行的正常部分。此外, 不会捕获堆栈跟踪,因此比典型的异常更有效。

另外一个例外是 $ b

另一个区别在于异常可以用于控制流。需要跳出三个嵌套循环?没问题 - 抛出一个异常(没有堆栈跟踪)并将其捕获到外部。需要跳出五个嵌套的方法调用?没问题!或者不提供任何类似的东西。



就是说,正如您指出的那样,有许多相似之处。你可以传回信息(尽管或者使得这些不重要,而检查的异常让你编写自己的类来存储任何你想要的额外信息)。你可以传递或者,或者你可以将它折叠到其他东西等等。



:虽然您可以使用或者完成相同的操作,并检查与显式错误处理有关的异常,但它们在实践中相对不同。特别是, 可以很容易地创建并传回不同的状态,而检查异常很好地绕过所有正常的控制流程,希望能够回到某个非常特殊的地方条件可以明智地处理。

Beginning in Scala and reading about Either I naturally comparing new concepts to something I know (in this case from Java). Are there any differences from the concept of checked exceptions and Either?

In both cases

  • the possibility of failure is explicitly annotated in the method (throws or returning Either)
  • the programmer can handle the error case directly when it occurs or move it up (returning again an Either)
  • there is a way to inform the caller about the reason of the error

I suppose one uses for-comprehensions on Either to write code as there would be no error similar to checked exceptions.

I wonder if I am the only beginner who has problems to see the difference.

Thanks

解决方案

Either can be used for more than just exceptions. For example, if you were to have a user either type input for you or specify a file containing that input, you could represent that as Either[String, File].

Either is very often used for exception handling. The main difference between Either and checked exceptions is that control flow with Either is always explicit. The compiler really won't let you forget that you are dealing with an Either; it won't collect Eithers from multiple places without you being aware of it, everything that is returned must be an Either, etc.. Because of this, you use Either not when maybe something extraordinary will go wrong, but as a normal part of controlling program execution. Also, Either does not capture a stack trace, making it much more efficient than a typical exception.

One other difference is that exceptions can be used for control flow. Need to jump out of three nested loops? No problem--throw an exception (without a stack trace) and catch it on the outside. Need to jump out of five nested method calls? No problem! Either doesn't supply anything like this.

That said, as you've pointed out there are a number of similarities. You can pass back information (though Either makes that trivial, while checked exceptions make you write your own class to store any extra information you want); you can pass the Either on or you can fold it into something else, etc..

So, in summary: although you can accomplish the same things with Either and checked exceptions with regards to explicit error handling, they are relatively different in practice. In particular, Either makes creating and passing back different states really easy, while checked exceptions are good at bypassing all your normal control flow to get back, hopefully, to somewhere that an extraordinary condition can be sensibly dealt with.

这篇关于是等同于检查异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆