0% found this document useful (0 votes)
22 views2 pages

Demo1: Phone (S D NG Exception) : T5: Objectives

Module 8 will cover more on classes including class variables, nested classes. Module 9 will introduce exceptions, how to handle exceptions in Java, user defined exceptions, and assertions. The document then provides two demos: the first uses exceptions to handle invalid input for a phone number array. The second demo defines a custom exception class called MyDemoException and demonstrates how to throw and catch it.

Uploaded by

flow_over
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Demo1: Phone (S D NG Exception) : T5: Objectives

Module 8 will cover more on classes including class variables, nested classes. Module 9 will introduce exceptions, how to handle exceptions in Java, user defined exceptions, and assertions. The document then provides two demos: the first uses exceptions to handle invalid input for a phone number array. The second demo defines a custom exception class called MyDemoException and demonstrates how to throw and catch it.

Uploaded by

flow_over
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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