Advertisement
zemiak

custom exception

Jun 1st, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. // import java.util.sanner;
  2. import java.util.Scanner;
  3.  
  4. public class checkEligibility {
  5.   public static void main(String[] args) {
  6.     // system.out.printin("Customer_number\tAge\tUnit");
  7.     System.out.println("id\tAge\tUnit");
  8.     int age[]  = {50,  60, 70, 30};
  9.     int unit[] = {300,200,100,400};
  10.  
  11.     for (int counter = 0; counter < age.length; counter++) {
  12.       // system.out.println(counter + "\t" + age[counter] + "\t" + unit[counter]);
  13.       System.out.println(counter + "\t" + age[counter] + "\t" + unit[counter]);
  14.     }
  15.     for (int counter = 0; counter < age.length; counter++) {
  16.       if (age[counter] < 55 && unit[counter] > 250) {
  17.         // <added>
  18.         System.out.print("id " +counter +": ");
  19.         // </added>
  20.         // JavaException();
  21.         new JavaException();
  22.       }
  23.     }
  24.   }
  25. }
  26.  
  27. class JavaException {
  28.   // public static void main(String args[]) {
  29.   JavaException() {
  30.     try {
  31.       throw new MyException(2);
  32.       // throw is used to create a new exception and throw it.
  33.     } catch (MyException e) {
  34.         System.out.println(e);
  35.     }
  36.   }
  37. }
  38.  
  39. // <added>
  40. class MyException extends Exception {
  41.   private static final long serialVersionUID =
  42.     1L; //eg.
  43.   public MyException(int n) {
  44.     //... ?
  45.   }
  46. // </added>
  47.  
  48.   public String toString(){
  49.     return ("The consumer is not eligible for getting the discount.");
  50.   }
  51. }
  52.  
  53. /* Output:
  54. id      Age     Unit
  55. 0       50      300
  56. 1       60      200
  57. 2       70      100
  58. 3       30      400
  59. id 0: The consumer is not eligible for getting the discount.
  60. id 3: The consumer is not eligible for getting the discount.
  61. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement