-
-
Save krystian-booker/c673955c539ef84c922c1bcb97440c65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Week6Task2 { | |
| // Method to divide two numbers | |
| public static double divide(int num1, int num2) { | |
| try { | |
| return (double) num1 / num2; | |
| } catch (ArithmeticException e) { | |
| System.out.println("Cannot divide by zero."); | |
| return 0; | |
| } | |
| } | |
| public static void main(String[] args) { | |
| // Call the divide method with parameters that will not cause an exception | |
| System.out.println("Result: " + divide(10, 2)); | |
| // Call the divide method with parameters that will cause an exception | |
| System.out.println("Result: " + divide(10, 0)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment