|
| 1 | +package com.company; |
| 2 | + |
| 3 | + |
| 4 | +public class try_catch { |
| 5 | + public static void main(String[] args) { |
| 6 | + int a = 3000; |
| 7 | + int b = 0; |
| 8 | + try{ |
| 9 | + int c = a/b; |
| 10 | + System.out.println("The result is:" +c); |
| 11 | + } |
| 12 | + catch (Exception e){ |
| 13 | + System.out.println("We failed to divide. Reason: "); |
| 14 | + System.out.println(e); |
| 15 | + System.out.println("End of the program"); |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// Specific exceptions |
| 21 | + |
| 22 | +package com.company; |
| 23 | + |
| 24 | +import java.util.Scanner; |
| 25 | + |
| 26 | +public class Specific_exceptions { |
| 27 | + public static void main(String[] args) { |
| 28 | + int[] marks = new int[3]; |
| 29 | + marks[0]=30; |
| 30 | + marks[1]=38; |
| 31 | + marks[2]=49; |
| 32 | + Scanner sc = new Scanner(System.in); |
| 33 | + System.out.println("Enter the array index: "); |
| 34 | + int ind = sc.nextInt(); |
| 35 | + |
| 36 | + System.out.println("Enter the number you want to dic=vide with: "); |
| 37 | + int number = sc.nextInt(); |
| 38 | + try{ |
| 39 | + System.out.println("The value at array index entered is: " +marks[ind]); |
| 40 | + System.out.println("The value of array-value/number is: "+marks[ind]/number); |
| 41 | + } |
| 42 | + catch (ArithmeticException e){ |
| 43 | + System.out.println("Some exception occured!"); |
| 44 | + System.out.println(e); |
| 45 | + } |
| 46 | + catch (ArrayIndexOutOfBoundsException e){ |
| 47 | + System.out.println("Some exception occured!"); |
| 48 | + System.out.println(e); |
| 49 | + } |
| 50 | + catch (Exception e){ |
| 51 | + System.out.println("Some other exception occured!"); |
| 52 | + System.out.println(e); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +//Nested try-catch |
| 59 | + |
| 60 | +package com.company; |
| 61 | + |
| 62 | +import java.util.Scanner; |
| 63 | + |
| 64 | +public class Nested_try_catch { |
| 65 | + public static void main(String[] args) { |
| 66 | + int[] marks = new int[3]; |
| 67 | + marks[0]=30; |
| 68 | + marks[1]=38; |
| 69 | + marks[2]=49; |
| 70 | + Scanner sc = new Scanner(System.in); |
| 71 | + boolean flag = true; |
| 72 | + while (flag) { |
| 73 | + |
| 74 | + System.out.println("Enter the array index: "); |
| 75 | + int ind = sc.nextInt(); |
| 76 | + try { |
| 77 | + System.out.println("Welcome to this world"); |
| 78 | + try { |
| 79 | + System.out.println(marks[ind]); |
| 80 | + flag = false; |
| 81 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 82 | + System.out.println("Sorry this index does not exist!"); |
| 83 | + System.out.println("Exception in level 2"); |
| 84 | +// flag = false; |
| 85 | + } |
| 86 | + } catch (Exception e) { |
| 87 | + System.out.println("Exception in level 1"); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +//Exception class |
| 95 | + |
| 96 | +package com.company; |
| 97 | + |
| 98 | +import java.util.Scanner; |
| 99 | + |
| 100 | +class MyException extends Exception{ |
| 101 | + @Override |
| 102 | + public String toString(){ |
| 103 | + return super.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getMessage() { |
| 108 | + return super.getMessage(); |
| 109 | + } |
| 110 | +} |
| 111 | +public class Exception_class { |
| 112 | + public static void main(String[] args) { |
| 113 | + int a; |
| 114 | + Scanner sc = new Scanner(System.in); |
| 115 | + a=sc.nextInt(); |
| 116 | + if (a<9){ |
| 117 | + try { |
| 118 | + throw new MyException(); |
| 119 | + } |
| 120 | + catch(Exception e){ |
| 121 | + System.out.println(e.getMessage()); |
| 122 | + System.out.println(e.toString()); |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +//throw-throws kwywords |
| 131 | + |
| 132 | +package com.company; |
| 133 | + |
| 134 | +class NegativeRadiusException extends Exception{ |
| 135 | + @Override |
| 136 | + public String toString() { |
| 137 | + return "Radius cannot be negative"; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String getMessage() { |
| 142 | + return "Radius cannot be negative"; |
| 143 | + } |
| 144 | +} |
| 145 | +public class Throw_throws { |
| 146 | + public static double area(int r) throws NegativeRadiusException{ |
| 147 | + if (r<0){ |
| 148 | + throw new NegativeRadiusException(); |
| 149 | + } |
| 150 | + double result = Math.PI*r*r; |
| 151 | + return result; |
| 152 | + } |
| 153 | + public static int divide(int a, int b) throws ArithmeticException{ |
| 154 | + int result = a/b; |
| 155 | + return result; |
| 156 | + } |
| 157 | + public static void main(String[] args) { |
| 158 | + try { |
| 159 | +// int c = divide(3,3); |
| 160 | +// System.out.println(c); |
| 161 | + double ar = area(0); |
| 162 | + System.out.println(ar); |
| 163 | + } |
| 164 | + catch (Exception e){ |
| 165 | + System.out.println("Exception"); |
| 166 | + } |
| 167 | +// double ar = area(5); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +// finally keyword |
| 173 | + |
| 174 | +package com.company; |
| 175 | + |
| 176 | +public class Finally { |
| 177 | + public static int greet() { |
| 178 | + try { |
| 179 | + int a = 60; |
| 180 | + int b = 4; |
| 181 | + int c = a / b; |
| 182 | + return c; |
| 183 | + } catch (Exception e) { |
| 184 | + System.out.println(e); |
| 185 | + } finally { |
| 186 | + System.out.println("This is the end of this function"); |
| 187 | + } |
| 188 | + return 0; |
| 189 | + } |
| 190 | + public static void main(String[] args) { |
| 191 | +// try { |
| 192 | +// int a = 5; |
| 193 | +// int b = 0; |
| 194 | +// int c = a/b; |
| 195 | +// } |
| 196 | +// catch (Exception e){ |
| 197 | +// System.out.println(e); |
| 198 | +// } |
| 199 | +// finally { |
| 200 | +// System.out.println("This is the end of this program"); |
| 201 | + int k = greet(); |
| 202 | + System.out.println(k); |
| 203 | + } |
| 204 | + } |
0 commit comments