Q:

Kotlin program of arithmetic exception handling using try-catch block

0

In this program, we will perform an arithmetic operation and handle arithmetic exceptions using a try-catch block.

Syntax for try-catch block:

try {
   // code that can throw exception
} catch(e: ExceptionName) {
   // catch the exception and handle it
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

// Importing the ArithmeticException class
import kotlin.ArithmeticException

fun main(args : Array<String>){
	var x = 10
	var y = 3
	try{
		println("10/3: " + x/y)

		x = 10
		y = 0 
		println("10/0: " + x/y)
	}
	catch(e: ArithmeticException){
		// caught and handles it
		println("Divide by zero exception")
	}
}

Output:

10/3: 3
Divide by zero exception

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now