|
| 1 | +packagecom; |
| 2 | +importjava.io.*; |
| 3 | +importjava.lang.Exception; |
| 4 | +importjava.io.File; |
| 5 | +importjava.io.FileInputStream; |
| 6 | +importjava.io.FileReader; |
| 7 | + |
| 8 | +publicclassWithExceptionHandling { |
| 9 | + |
| 10 | +//public static void main(String[] args) { |
| 11 | +// try{ |
| 12 | +//code that may raise exception |
| 13 | +//int data=100/0; // ArithmeticException |
| 14 | +//String s=null; // NullPointerException |
| 15 | +//System.out.println(s.length()); |
| 16 | +//String s="abc"; |
| 17 | +//int i=Integer.parseInt(s); //NumberFormatException |
| 18 | +//int a[]=new int[5]; |
| 19 | +//a[10]=50; //ArrayIndexOutOfBoundsException |
| 20 | +// }catch(ArithmeticException e){ |
| 21 | +// System.out.println(e); |
| 22 | +// } |
| 23 | +// System.out.println("outside of exception handling"); |
| 24 | + |
| 25 | +//multiple catch |
| 26 | +//try{ |
| 27 | +// int a[]=new int[5]; |
| 28 | +// int b = 30/0; |
| 29 | +// a[5]=b; |
| 30 | +// } |
| 31 | +// catch(ArithmeticException e) |
| 32 | +// { |
| 33 | +// System.out.println("Arithmetic Exception occurs"); |
| 34 | +// } |
| 35 | +// catch(ArrayIndexOutOfBoundsException e) |
| 36 | +// { |
| 37 | +// System.out.println("ArrayIndexOutOfBounds Exception occurs"); |
| 38 | +// } |
| 39 | +// catch(Exception e) |
| 40 | +// { |
| 41 | +// System.out.println("Parent Exception occurs "); |
| 42 | +// } |
| 43 | +// System.out.println("rest of the code"); |
| 44 | + |
| 45 | +//nested try |
| 46 | +// try{ |
| 47 | +// try{ |
| 48 | +// System.out.println("going to divide"); |
| 49 | +// int b =39/0; |
| 50 | +// }catch(ArithmeticException e){System.out.println(e);} |
| 51 | +// |
| 52 | +// try{ |
| 53 | +// int a[]=new int[5]; |
| 54 | +// a[5]=4; |
| 55 | +// }catch(ArrayIndexOutOfBoundsException e){System.out.println(e);} |
| 56 | +// |
| 57 | +// System.out.println("other statement"); |
| 58 | +// }catch(Exception e){System.out.println("handled");} |
| 59 | +// |
| 60 | +// System.out.println("normal flow.."); |
| 61 | + |
| 62 | +//finally with or without exception handling |
| 63 | + |
| 64 | +// try{ |
| 65 | +// int data=25/0; |
| 66 | +// System.out.println(data); |
| 67 | +//}catch(NullPointerException e){ |
| 68 | +//}catch(ArithmeticException e){ |
| 69 | +// System.out.println(e); |
| 70 | +//}finally{ |
| 71 | +//System.out.println("finally block is always executed"); |
| 72 | +//} |
| 73 | +// System.out.println("rest of the code..."); |
| 74 | + |
| 75 | +//FileNotFoundException |
| 76 | + |
| 77 | +//try { |
| 78 | +//FileReader fl = new FileReader("C:\\Users\\Faris\\Desktop\\TXT DOCS\\ISMAI.txt"); |
| 79 | +//int i = fl.read(); |
| 80 | +//while(i != -1) { |
| 81 | +//System.out.println((char)i); |
| 82 | +//} |
| 83 | +//fl.close(); |
| 84 | +//}catch(Exception e){ |
| 85 | +//System.out.println(e); |
| 86 | +//} |
| 87 | +// } |
| 88 | + |
| 89 | +// throws |
| 90 | + |
| 91 | +//public static void method() throws FileNotFoundException { |
| 92 | +// |
| 93 | +// FileReader file = new FileReader("C:\\Users\\Faris\\Desktop\\TXT DOCS\\ISMAI.txt"); |
| 94 | +// BufferedReader fileInput = new BufferedReader(file); |
| 95 | +// |
| 96 | +// |
| 97 | +// throw new FileNotFoundException(); |
| 98 | +// |
| 99 | +// } |
| 100 | +// public static void main(String args[]){ |
| 101 | +// try |
| 102 | +// { |
| 103 | +// method(); |
| 104 | +// } |
| 105 | +// catch (Exception e) |
| 106 | +// { |
| 107 | +// System.out.println(e); |
| 108 | +// } |
| 109 | +// System.out.println("rest of the code..."); |
| 110 | +// } |
| 111 | + |
| 112 | +//} |
| 113 | +} |