Advertisement
jasonpogi1669

How to implement exception and assertion using Java (MP 8)

Mar 1st, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package main;
  7.  
  8. /**
  9.  *
  10.  * @author markjasongalang
  11.  */
  12. class FindArray {
  13.     public void FindArrayFunction(int[] array, int array_size) {
  14.         try {
  15.             for (int i = 0; i < array_size; i++) {
  16.                 System.out.print(array[i] + " ");
  17.             }
  18.             System.out.println();
  19.         } catch (ArrayIndexOutOfBoundsException e) {
  20.             System.out.println("\nEncountered an array index out of bounds exception.");
  21.         } catch (NullPointerException e) {
  22.             System.out.println("\nEncountered a null pointer exception.");
  23.         } catch (RuntimeException e) {
  24.             System.out.println("\nEncountered a runtime exception.");
  25.         }
  26.     }
  27. }
  28.  
  29. public class Main {
  30.     public static void main(String[] args) {
  31.         FindArray find_array_object = new FindArray();
  32.         int[] array = {1, 2, 3, 4, 5};
  33.         find_array_object.FindArrayFunction(array, array.length);
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement