0

I am writing a simple java code to demonstrate various types of exceptions and how they're handled.

So far, I decided to try a custom exception with my own condition and a pre-defined IndexOOB exception to test.

Below is the code for the same:

import java.util.*;

class customException extends Exception
{//custom exceptionto check
    customException(String s)
    {
        super(s);
    }
}

public class lab7 {
    public void display_names(String name[],int roll[])
    {//checking for indexOOB exception
        Scanner sc = new Scanner(System.in);
        int r;
        try
        {
            System.out.println("Enter roll number to display details");
            r = sc.nextInt();
            System.out.println(name[r-1]+", "+roll[r-1]);
        }
        catch(IndexOutOfBoundsException e)
        {
            System.out.println("Roll number out of bounds!");
        }
        finally
        {
            sc.close();
        }
    }
    public void num500()
    {//throwing custom exception
        int num;
        Scanner sc= new Scanner(System.in);
        try
        {
            System.out.println("Enter a number:");
            num = sc.nextInt();
            if(num<500)
            {
                throw new customException("Sharru.bhang.NumberLessThan500Exception");
            }
        }
        catch(customException e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            sc.close();
        }
    }
    public static void main(String[] args) 
    {
        //code 1
        lab7 ob = new lab7();
        int roll[] = {1,2,3,4,5,6,7,8,9,10};
        String name[] = {"Khxshi","Sharru","Dev","Aman","Yasho","Bhari","Owais","Pratham","Shivansh","Chinmay"};
        ob.display_names(name, roll);

        //code 2 in separate file

        //code 3
        ob.num500();
    }
}

When i call these functions without calling the other, I do not encounter any problems, but when I try to run both the methods at the same time, I get the following exception:

Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at lab7.num500(lab7.java:38)
        at lab7.main(lab7.java:64)

Can anybody explain what I am doing wrong? Thanks!

  • 1
    `sc.close();` doesn't close only Scanner but also *resource* which Scanner is reading, here `System.in`. So when you create another Scanner to read from already closed System.in Scanner informs you that it can't read from it by throwing this exception. Solution is to not close Scanner in your methods, but create one scanner (in `main` method) then pass it as argument to methods which use it, like `display_names`. This way you only need to close Scanner at the end of main method, when it is no longer needed. – Pshemo Apr 13 '23 at 10:19

0 Answers0