You are on page 1of 2

T5: Objectives

Module 8 - More on Classes Class Variables Nested Class

Module 9 Exceptions Introduction to Exceptions Exception handling in Java User defined exceptions Assertions

T5: Demo

Demo1: Phone (S dng Exception)


Scanner input = new Scanner(System.in); try { System.out.println("Enter the number of mobile numbers to store:"); int size = input.nextInt(); phoneNumbers = new int[size]; for(int index = 0;index < phoneNumbers.length;index++) { System.out.println("Enter a phone number:"); phoneNumbers[index] = input.nextInt(); } } catch(NegativeArraySizeException e) { System.out.println("Exception occurred - " + "Array size should be a positive value."); } catch(InputMismatchException e) { System.out.println("Exception occurred - Data type mismatch." + " Enter non-zero numeric value and try again."); } catch(Exception e) { System.out.println("Exception occurred - " + e.getMessage()); }

Demo2: T vit 1 exception ring MyDemoException


public class MyDemoException extends Exception { public MyDemoException() { super(); } public MyDemoException(int number){

super("This is my exception " + number ); } }

Cch s dng:
int temp; try { System.out.println("Enter new capacity of the hotel:"); temp = input.nextInt(); if(temp <= 0) { throw new MyDemoException(0); } } catch (MyDemoException e) { System.out.println("Enter a non-zero positive number"); }

You might also like