4.EXCEPTION HANDLING IN JAVA

  4.EXCEPTION HANDLING IN JAVA Source Code: 





import java.io.*; class Predefined{ void ArithmeticExcp(){ try{ int a=20,b=0,c; c=a/b; System.out.println("Result:"+c); } catch(ArithmeticException e){ System.out.println("Arithmetic Exception:Division by zero"); } } void StrindOutofBoundExcp(){ try{ String s="Java Programming"; char c=s.charAt(25); System.out.println(c); } catch(StringIndexOutOfBoundsException e) { System.out.println("String out of Bound Exception:character Index out of Range"); } } void ArrayOutofBoundExcp(){ try{ int a[]=new int[5]; a[8]=9; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array out of Bound Exception: Index out of Range"); } } void FileNotFoundExcp() throws FileNotFoundException{ try{ File file=new File("D://sample.txt"); FileReader fr=new FileReader(file); } catch(FileNotFoundException e) { System.out.println("File Not Found Exception: File does not exist"); } } } public class PreDefinedException { public static void main(String[] args) throws FileNotFoundException { Predefined p=new Predefined(); System.out.println("\t\t\tPredefined Exception Handling in Java"); System.out.println("\t\t\t*************************************"); p.ArithmeticExcp(); p.StrindOutofBoundExcp(); p.FileNotFoundExcp(); p.ArrayOutofBoundExcp(); } } Output: Predefined Exception Handling in Java ************************************* Arithmetic Exception:Division by zero String out of Bound Exception:character Index out of Range File Not Found Exception: File does not exist Array out of Bound Exception: Index out of Range

Comments