scala throws an exception

Throwing an exception
we can in one method, an exception is thrown. And Java syntax similar, use throw new Exception ...
an example of | throws an exception
example illustrates
throw an exception in the main method
reference code:

  def main(args: Array[String]): Unit = {
    throw new Exception("这是一个异常")
  }

Exception in thread "main" java.lang.Exception: 这是一个异常
    at ForDemo$.main(ForDemo.scala:3)
    at ForDemo.main(ForDemo.scala)

scala not need to declare an exception to be thrown in the way, it has been resolved then Java is considered to be a design failure checked exceptions.
Here is the Java code:

public static void main(String[] args) throws Exception {
    throw new Exception("这是一个异常");
}

Here Insert Picture Description
result:
Here Insert Picture Description

Published 151 original articles · won praise 339 · Views 230,000 +

Guess you like

Origin blog.csdn.net/qq_45765882/article/details/104335732