Scala中的else语句的例外

发布于 2025-02-09 16:34:13 字数 358 浏览 2 评论 0原文

if ( option != "AB" || option != "BC"|| option != "CD") {
  try {
    var option = dbutils.widgets.get("option")
  }

  catch {
  case e: Exception => println("The option shoud be following AB, BC or CD " + option)
}
}

我试图在Scala中使用异常

,但是,当我使用诸如

“ XY”或其他值之外的任何其他值之外的值时,小部件值并没有引发异常。

请有人可以让我知道我在这里做错了什么。谢谢

if ( option != "AB" || option != "BC"|| option != "CD") {
  try {
    var option = dbutils.widgets.get("option")
  }

  catch {
  case e: Exception => println("The option shoud be following AB, BC or CD " + option)
}
}

I am trying to use exception in scala

However, the widget value is not throwing an exception when I use the value such as

"XY" or any other value than AB, BC or CD.

Please can someone let me know what I am doing wrong here. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2025-02-16 16:34:13

如果AB,BC& CD是有效的值,那么您的谓词应该看起来像这样:

if(option == "AB" || option == "BC" || option == "CD")

另外,您可以使用需要函数来执行谓词并在输入不匹配时抛出异常:

def getWidget(option: String) = try {
  require(option == "AB" || option == "BC"|| option == "CD", s"The option shoud be following AB, BC or CD instead got $option")
  dbutils.widgets.get("option")
  } catch { case e: Exception => println(e) }

If AB,BC & CD are the valid values, then your predicate should look like this:

if(option == "AB" || option == "BC" || option == "CD")

Also, you can use the require function to enforce the predicate and throw an exception if the input doesn't match:

def getWidget(option: String) = try {
  require(option == "AB" || option == "BC"|| option == "CD", s"The option shoud be following AB, BC or CD instead got $option")
  dbutils.widgets.get("option")
  } catch { case e: Exception => println(e) }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文