Lab Da 4 - DA4 PDF

Title Lab Da 4 - DA4
Course JAVA Programming
Institution Vellore Institute of Technology
Pages 15
File Size 724.2 KB
File Type PDF
Total Downloads 53
Total Views 135

Summary

DA4...


Description

JAVA PROGRAMMING Lab DA 4 NAME : HENIL SATRA

COURSE : CSE1007

REG. NO. : 18BCE2281

SLOT : L23 + L24

Exceptions : 1. Arithmetic Exception Code : class ArithmeticException18BCE2281 { public static void main(String args[]) { try { int a = 30, b = 0; int c = a/b; System.out.println ("Result = " + c); } catch(ArithmeticException e) { System.out.println ("Can't divide a number by 0"); } } } Output :

2. Null Pointer Exception Code : class NullPointer18BCE2281 { public static void main(String args[]) { try { String a = null; System.out.println(a.charAt(0)); } catch(NullPointerException e) { System.out.println("NullPointerException has occured"); } } } Output :

3. Number Format Exception Code : class NumberFormat18BCE2281 { public static void main(String args[]) { try { int num = Integer.parseInt ("aditya") ; System.out.println(num); } catch(NumberFormatException e) { System.out.println("Number format exception"); } Output :

4. Array Index Out Of Bound Exception Code : class ArrayIndexOutOfBound18BCE2281 { public static void main(String args[]) { try{ int a[] = new int[5]; a[6] = 9; } catch(ArrayIndexOutOfBoundsException e){ System.out.println ("Array Index is Out Of Bounds"); } } } Output :

5. Multiple Catch : Code : class MultipleCatch18BCE2281 { public static void main(String[] args) { try{ int a[]=new int[5]; a[5]=30/0; } catch(ArithmeticException e) { System.out.println("Arithmetic Exception occurs"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBounds Exception occurs"); } catch(Exception e) { System.out.println("Parent Exception occurs"); } System.out.println("rest of the code"); } }

Output :

6. Nested Try Code: class nestedTry18BCE2281{ public static void main(String args[]){ try{ try{ System.out.println("going to divide"); int b =18/0; }catch(ArithmeticException e){System.out.println(e);} try{ int a[]=new int[5]; a[5]=4; }catch(ArrayIndexOutOfBoundsException e){System.out.println(e);} System.out.println("other statement"); }catch(Exception e){System.out.println("handeled");} System.out.println("normal flow.."); } } Output :

7. Finally without exception handled Code : class TestFinally18BCE2281 { public static void main(String args[]){ try{ int data=25/0; System.out.println(data); } catch(NullPointerException e){System.out.println(e);} finally{System.out.println("finally block is always executed");} System.out.println("rest of the code..."); } } Output :

8. Finally with exception handled Code : class FinallyBlock18BCE2281{ public static void main(String args[]){ try{ int data=25/0; System.out.println(data); }

catch(ArithmeticException e){System.out.println(e);} finally{System.out.println("finally block is always executed");} System.out.println("rest of the code..."); } } Output :

9. Throw Exception Code : class TestThrow18BCE2281{ static void validate(int age){ if(age...


Similar Free PDFs