-3
import java.util.*;
class ArrayOverflowException extends Exception{
    ArrayOverflowException(){
        super(); 
    }
    static int i;
    static void check(int i) throws ArrayOverflowException{
        if (i>5) {
            throw new ArrayOverflowException("Array Overflow");
        return;
}
}
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    int a[]=new int[5];
    System.out.println("Enter the elemets of the array. #MAX 6 ELEMENTS#");
    try{
        for (int i=0;i<10;i++) {
            ArrayOverflowException.check(i);
            a[i]=input.nextInt();
        }
    }catch (ArrayOverflowException e) {
        System.out.println("Exception handled"+e);
    }
}
}

I have been trying to create my own user defined exception in Java that gives an error cannot find symbol in line 9 . Please help.

Line 9 is throw new ArrayOverflowException("Array Overflow");

toha
  • 5,095
  • 4
  • 40
  • 52
Palak Bansal
  • 810
  • 12
  • 26

2 Answers2

2

1) add constructor with string param

ArrayOverflowException(String message){
    super(message); 
}

2) remove return, it's unreachable code

return;

Also check if you want to check i or value from user input?

Olesia
  • 144
  • 4
1

There are 2 problems here:

  1. You need to create a constructor that accepts a String parameter:

    ArrayOverflowException(String message) {
        super(message);
    }
    
  2. You can't return after throw, since it immediately jumps to the nearest catch block, which in this case is outside the method.

shmosel
  • 49,289
  • 6
  • 73
  • 138