Advertisement
makispaiktis

18. Try and catch exceptions

May 31st, 2022 (edited)
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.rmi.server.ExportException;
  2. import java.util.*;
  3.  
  4. public class ExceptionHandler {
  5.  
  6.     // Main Function
  7.     public static void main(String[] args) {
  8.         Scanner input = new Scanner(System.in);
  9.         int x = 1;
  10.  
  11.         do {
  12.             try {
  13.                 System.out.print("Enter dividend: ");
  14.                 int n1 = input.nextInt();
  15.                 System.out.print("Enter divisor: ");
  16.                 int n2 = input.nextInt();
  17.                 int n = n1 / n2;
  18.                 System.out.println(n);
  19.                 x = 2;
  20.             }
  21.             catch (Exception e) {
  22.                 System.out.println("You can't do that");
  23.             }
  24.         } while (x == 1);
  25.  
  26.         System.out.println("Finally! You understood that you can't divide by 0");
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement