Skip to content

Instantly share code, notes, and snippets.

@krystian-booker
Created August 3, 2023 00:52
Show Gist options
  • Select an option

  • Save krystian-booker/c673955c539ef84c922c1bcb97440c65 to your computer and use it in GitHub Desktop.

Select an option

Save krystian-booker/c673955c539ef84c922c1bcb97440c65 to your computer and use it in GitHub Desktop.
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