🌙 DARK

Exception Handling Java

 

ARRAYINDEXOUTOFBOUNDSEXCEPTION

ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException occurs when the program tries to access the array beyond its size. 

Write a program to manipulate array operation by handling ArrayIndexOutOfBoundsException

Create an array of size 100 and assume it as seat array. Get the tickets to be booked from
 the user and handle any exception that occurs in Main Class. At last display all the tickets booked.
 


Input and Output format:

The first line of input consists of an integer which corresponds to the number of seats to be booked.
The next n lines of input consist of the integer which corresponds to the seat number. The seat number starts from 1 to 100.
If the exception is thrown, then display the exception with the index number. Else display all the tickets booked
Refer to sample Input and Output for formatting specifications.

[All Texts in bold corresponds to the input and rest are output]

Sample Input and Output 1:


Enter the number of seats to be booked:
5
Enter the seat number 1
23
Enter the seat number 2
42
Enter the seat number 3
65
Enter the seat number 4
81
Enter the seat number 5
100
The seats booked are:
23
42
65
81
100

Sample Input and Output 2:

Enter the number of seats to be booked:
4
Enter the seat number 1
12
Enter the seat number 2
101
java.lang.ArrayIndexOutOfBoundsException: 100

 



Problem Requirements:

Java

KeywordMin CountMax Count
ArrayIndexOutOfBoundsException1-
KeywordMin CountMax Count
catch--
KeywordMin CountMax Count
try1-




PROGRAM:-
@MRProgrammer89 


CREATE CLASS < Main :

import java.util.Scanner;

public class Main {
public static void main(String args[]) throws ArrayIndexOutOfBoundsException{
        
        Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of seats to be booked:");
int booked = sc.nextInt();
    int [] seat = new int[100];
    try 
        {
            for(int i= 1; i<=booked; i++) {
System.out.println("Enter the seat number "+(i));
int j = sc.nextInt();
seat[j-1]=1;
            }
            System.out.println("The seats booked are:");
            for(int i= 0; i<100; i++) 
            {
            if(seat[i] == 1) {
            System.out.println(i+1);
           
            }}
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}

}
}








ARITHMETIC EXCEPTION

Arithmetic Exception
   
An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at runtime, it disrupts the normal flow of the program. For example, there are 10 statements in your program and there occurs an exception at statement 5, the rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, the rest of the statement will be executed. That is why we use exception handling.
 
Write a program to obtain the cost for 'n' days of an item and n as input and calculate the cost per day for the item. In case, zero is given as input for n, an arithmetic exception is thrown, handle the exception and prompt the user accordingly.
 
Consider a driver class called Main. In the main method, obtain input from the user and store the values in integer type. Handle exception if one occurs.
 
The link to download the template code is given below
Code Template


Input format:

The first line of input is an integer which corresponds to the cost of the item for n days.
The second line of input is an integer which corresponds to the value n.


Output format:

If the value of n is zero throws an exception.
Otherwise, print the integer output which corresponds to the cost per day of the item.

 
[All text in bold corresponds to the input and rest corresponds to the output]

Sample Input  and Output 1:

Enter the cost of the item for n days
100
Enter the value of n
 0
java.lang.ArithmeticException: / by zero

Sample Input and Output 2:

Enter the cost of the item for n days
100
Enter the value of n
20
Cost per day of the item is 5

 



Problem Requirements:

Java

KeywordMin CountMax Count
catch1-
KeywordMin CountMax Count
ArithmeticException1-
KeywordMin CountMax Count
try1-

PROGRAM:-
@MRProgrammer89 


CREATE CLASS < Main > :

import java.util.Scanner;
public class Main {
    public static void main(String argd[]) throws ArithmeticException {
    Scanner sc = new Scanner(System.in);
System.out.println("Enter the cost of the item for n days");
int a = sc.nextInt();
System.out.println("Enter the value of n");
int b = sc.nextInt();
try {
System.out.println("Cost per day of the item is "+a/b);
}
catch(ArithmeticException e) {
System.out.println(e);
}
}}





@MRProgrammer89 






MOTIVATION :


    “The way we spend our time defines who we are 


Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


VISITE MORE BLOGS




THANK YOU....😊

Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form