How to resolve the algorithm Detect division by zero step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Detect division by zero step by step in the Scala programming language

Table of Contents

Problem Statement

Write a function to detect a   divide by zero error   without checking if the denominator is zero.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Detect division by zero step by step in the Scala programming language

Source code in the scala programming language

object DivideByZero extends Application {
  
  def check(x: Int, y: Int): Boolean = {
    try {
      val result = x / y
      println(result) 
      return false
    } catch {
      case x: ArithmeticException => {
        return true
      }
    }
  }
  
  println("divided by zero = " + check(1, 0))

  def check1(x: Int, y: Int): Boolean = {
    import scala.util.Try
    Try(y/x).isFailure
  }
  println("divided by zero = " + check1(1, 0))

}


  

You may also check:How to resolve the algorithm Take notes on the command line step by step in the CLU programming language
You may also check:How to resolve the algorithm Hash join step by step in the Julia programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the Scala programming language
You may also check:How to resolve the algorithm Terminal control/Positional read step by step in the Racket programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the FutureBasic programming language