Advertisement
NB52053

Exception

Aug 2nd, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Created by akashs on 3/20/17.
  5.  */
  6. public class Solution {
  7.  
  8.     public static int fun(int a, int b) throws OddEvenException,ArithmeticException
  9.     {
  10.         try {
  11.             if (b == 0) {
  12.                 ArithmeticException e = new ArithmeticException("You entere a zero as denominator");
  13.                 throw e;
  14.             }
  15.             if(b==1){
  16.                 OddEvenException e = new OddEvenException();
  17.                 throw e;
  18.             }
  19.         }catch (ArithmeticException e){
  20.  
  21.         }
  22.  
  23.         int res = a/b;
  24.         return res;
  25.     }
  26.  
  27.     public static void checkOD(int a) throws OddEvenException {
  28.         if(a%2==0){
  29.             throw new OddEvenException();
  30.         }
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         Scanner sc = new Scanner(System.in);
  35.  
  36.         int a = sc.nextInt();
  37.         int b = sc.nextInt();
  38.  
  39.         int[] ar = new int[5];
  40.  
  41.         try {
  42.             checkOD(a);
  43.             System.out.println("Odd");
  44.         } catch (OddEvenException e) {
  45.             System.out.println("Even");
  46.         }
  47.  
  48.         try {
  49.             fun(a,b);
  50.         } catch (OddEvenException e) {
  51.             e.printStackTrace();
  52.         }
  53.         catch (ArithmeticException e) {
  54.             e.printStackTrace();
  55.         }
  56.  
  57.         int res=0;
  58.         try {
  59.             try {
  60.             //res = a / b;
  61.  
  62.             System.out.println(res);
  63.  
  64.                 ar[a] = res;
  65.             }
  66.             catch (IndexOutOfBoundsException e){
  67.                 //System.out.println("input value is not valid.");
  68.                 System.out.println(e.getMessage());
  69.             }
  70.             System.out.println("First try block");
  71.         }
  72.  
  73.         catch (ArrayIndexOutOfBoundsException e){
  74.             System.out.println("Index value is not appropriate.");
  75.         }
  76. //        catch (ArithmeticException ex){
  77. //            System.out.println("You enter invalid number.");
  78. //
  79. //            System.out.println(ex.getMessage());
  80. //            System.out.println(ex);
  81. //            //res=0;
  82. //        }
  83. //        catch (Exception e){
  84. //            System.out.println("Exception occured");
  85. //        }
  86.  
  87.         finally {
  88.             System.out.println("Result is "+res);
  89.         }
  90.  
  91.  
  92.         System.out.println("Program execution complete");
  93.  
  94.     }
  95. }
  96.  
  97.  
  98. ////////////////////////////////////
  99.  
  100. public class OddEvenException extends Exception {
  101.     int a;
  102.     String status;
  103.  
  104.     public OddEvenException() {
  105.     }
  106.  
  107.     public OddEvenException(String message, String status) {
  108.         super(message);
  109.         this.status = status;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement