Safe divide

Safe divide

Easy Java Exceptions 23 views
Explanation Complexity

Problem Statement

Task: divide a by b and throw ArithmeticException when b is 0.

Input Format

Two integers a and b.

Output Format

Result of a / b
If b == 0, throw ArithmeticException.

Example

10 0 
ArithmeticException

Constraints

• a and b are integers

• Must manually check division by zero

Concept Explanation

Division by zero is not allowed.
If b == 0, we throw ArithmeticException.
Otherwise, return the result of division.

Step-by-Step Explanation

1.Read integers a and b.

2.If b == 0:

• Throw new ArithmeticException("Division by zero").

3.Else:

• Compute result = a / b.

4.Return result.

Concept Explanation

Division by zero is not allowed.
If b == 0, we throw ArithmeticException.
Otherwise, return the result of division.

Step-by-Step Explanation

1.Read integers a and b.

2.If b == 0:

• Throw new ArithmeticException("Division by zero").

3.Else:

• Compute result = a / b.

4.Return result.

Input / Output Format

Input Format
Two integers a and b.
Output Format
Result of a / b
If b == 0, throw ArithmeticException.
Constraints
• a and b are integers

• Must manually check division by zero

Examples

Input:
10 0
Output:
ArithmeticException

Example Solution (Public)

Java
static int safeDivide(int a,int b){if(b==0) throw new ArithmeticException();return a/b;}

Official Solution Code

static int safeDivide(int a,int b){if(b==0) throw new ArithmeticException();return a/b;}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.