scala方法抛出异常_Scala异常| Scala方法如何引发异常?

scala方法抛出异常

Scala异常 (Scala Exceptions)

Exception in programming occurs during execution of the program, the error occurs during the execution of the program. This error interrupts the execution sequence of the code. The exceptions are handled using bypassing the code to an exception handler. This handles the exceptions and reduces the occurrence of error.

在程序执行期间发生编程异常,而在程序执行期间发生错误。 该错误中断了代码的执行顺序。 通过将代码绕过异常处理程序来处理异常。 这样可以处理异常并减少错误的发生。

Exception Handling in Scala has the same working as exceptions of any other programming language but is implemented a bit differently. Scala supports only unchecked exceptions i.e. no checked exception are inbuilt for Scala.

Scala中的异常处理与任何其他编程语言的异常具有相同的工作方式,但实现方式略有不同。 Scala仅支持未检查的异常,即,Scala没有内置任何检查的异常。

Exceptions are inherited from throwable which also has a subclass of errors. And exceptions have a subclass called unchecked exception which is quite useful in programming.

异常是从throwable继承的,后者还具有错误的子类。 并且异常具有一个称为未检查异常的子类,这在编程中非常有用。

Scala异常处理 (Scala Exception Handling )

Exceptions are handled the same way as other programming languages like Java i.e. throwing an exception and then making for a block that catches it.

异常的处理方式与其他编程语言(如Java)相同,即抛出异常,然后创建一个捕获该异常的块。

How does Scala exception handling work?

Scala异常处理如何工作?

Suppose an exception occurs in your Scala code. The compiler holds execution and gives the flow to the exception handler, that does its work. And runs the code, otherwise, the program gets terminated.

假设您的Scala代码中发生异常。 编译器将保留执行,并将流程提供给异常处理程序,以完成其工作。 并运行代码,否则程序将终止。

Scala also uses the try-catch block to handle exceptions. As in java, Scala also uses try block to put the code that can have error and then use catch or finally block to handle the error thrown.

Scala还使用try-catch块来处理异常 。 与在Java中一样,Scala也使用try块放置可能有错误的代码,然后使用catch或finally块来处理抛出的错误。

Scala中抛出异常 (Throwing exception in Scala)

Throwing exceptions in Scala needs the keyword throw which creates an exception object.

Scala中的throwing exceptions需要关键字throw,它创建一个异常对象。

Try catch block

尝试抓挡

Scala also uses the try-catch block to handle the exceptions that may arise during the program execution.

Scala还使用try-catch块来处理程序执行过程中可能出现的异常。

Error handling using the try-catch block works in the following manner: the code that may give rise to another is footed in the try block. based on the type of exception the code Code throws the cache log has exception objects that are executed when the exception occurs.

使用try-catch块进行的错误处理以下列方式工作:可能引起另一个代码的代码位于try块中。 根据异常的类型,代码引发缓存日志的代码具有在异常发生时执行的异常对象。

For example, if we place a division in Scala try block and the expression evaluates to two division by zero it throws an arithmetic exception that needs to be handled. so in our Scala catch block, we will define an arithmetic exception that will be caught if an exception occurs and the following code will be executed based on the occurrence of the exception.

例如,如果我们在Scala try块中放置一个除法,并且该表达式的计算结果为零除以2,则它将引发需要处理的算术异常。 因此,在我们的Scala catch块中,我们将定义算术异常,该异常将在发生异常时捕获,并根据异常的发生执行以下代码。

Syntax:

句法:

    try{
	    //code that may contain an exception. 
    }
    catch {
	    case a : typeOfException =>
		    // code to be executed
    } 

Example:

例:

import java.io.IOException 
object MyClass 
{ 
	def main(args:Array[String]) 
	{   
	    var i= 3 
	    var j = 3 
		try
		{ 
			var N = 5/i - j
			println("The division is successful the value is :" + 5/(i-j))
		} 
		catch
		{ 
			case  e : ArithmeticException =>
			{ 
				println("Arithmetic exception occurred.") 
			}
		} 
       
	} 
} 

Output

输出量

The above code has 2 two outputs based on the value of I and J

case 1 : i= j
Arithmetic exception occurred

case 2 : i !=j
The division is successful the value is -5 

Note: **the value is evaluated based on the value entered in the code 

Sometimes in programming, there are cases when the exception occurred is not known to we have chances of being occurred or some exception that it does not have an exception class or a case to catch that. in this case, we will use The Finally keyword in the exception handling. the finally-block is a block of code in error handling that runs when no known exception case is matched i.e. it runs when catching face. the finally-code will ultimately even if there is an error or not. it will run with both cases when the try does not throw an exception, And when they try throws an exception and catch block runs.

有时在编程中,有些情况是我们不知道发生异常的机会,或者某些异常没有异常类或无法捕获的情况。 在这种情况下,我们将在异常处理中使用Final关键字finally块是错误处理中的代码块,当没有已知的异常情况匹配时运行,即在捕获面Kong时运行。 即使有没有错误, finally代码也将最终显示。 它将与这两种情况下运行时尝试未抛出异常,而当他们试图抛出一个异常,catch块运行。

Syntax:

句法:

    try{
	    //code that may contain an exception. 
    }
    catch {
	    case a : typeOfException =>
		    // code to be executed
    } 
    finally {
	    // This block will definitely run
    }

Example to show how catch block works?

展示catch块如何工作的示例?

import java.io.IOException 
object MyClass 
{ 
	def main(args:Array[String]) 
	{   
	    var i= 3 
	    var j = 3 
		try{ 
			var N = 5/i - j
			println("the division is successful the value is :" + 5/(i-j))
		} 
		catch{ 
			case  e : ArithmeticException =>{ 
				println("Arithmetic exception occurred.") 
			}
		} 
		finally {
		    println("The finally code that runs in all cases...")
		}
       
	} 
} 

Output

输出量

Arithmetic exception occurred.
The finally code that runs in all cases...

Code logic:

代码逻辑:

The above code is used to display how a finally statement runs when an error has occurred? In the example, we have a try block that contains a divide by zero arithmetic error, this error is then handled using the Scala catch block which prints "arithmetic exception occurred". After the execution of this catch block the finally block is invoked that prints "The Finally code that runs in all cases". After the execution of finally code the program goes back to its original flow.

上面的代码用于显示发生错误时finally语句如何运行 ? 在该示例中,我们有一个try块 ,其中包含一个除以零的算术错误,然后使用Scala catch块处理该错误,该代码显示“发生算术异常” 。 在执行该catch块之后,将调用finally块 ,该将显示“在所有情况下均运行的Final代码” 。 在执行完最终代码之后,程序将返回其原始流程。

翻译自: https://www.includehelp.com/scala/exceptions-how-scala-methods-throw-exceptions.aspx

scala方法抛出异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值