ADVERTISEMENT
ADVERTISEMENT

Kotlin program of arithmetic exception handling using try-catch block

Kotlin example to demonstrate arithmetic exception handling using a try-catch block.
Submitted by IncludeHelp, on April 11, 2022

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
}

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

Kotlin Exception Handling Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.