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("这是一个异常");
}
result: