2016-09-30 2 views
-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); 
    } 
} 
} 

나는 오류 cannot find symbol in line 9을주는 Java에서 내 자신의 사용자 정의 예외를 만들려고했습니다. 도와주세요.Java에서 사용자 정의 오류 만들기. 이 코드가 왜 작동하지 않습니까?

9 호는 새로운 ArrayOverflowException ("Array Overflow")을 던집니다.

+1

변화를'에 대한 (INT I = 0; i가 10 <; 내가 ++) {'내가 이미 시도 –

+0

가 그 일을하고 라인 9 인 차이 – Palak

+0

을하지 않았다? – shmosel

답변

1

1) 문자열 PARAM

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

2)를 제거 수익률 생성자를 추가, 그것은 도달 할 수없는 코드 또한

return; 

사용자 입력에서 I 또는 값을 확인하려면 확인입니까? 이 문제는 여기에있다

+0

감사! 그것은 효과가 있었다. – Palak

0

있습니다

  1. 당신은 String 매개 변수를 허용하는 생성자를 만들어야합니다

    ArrayOverflowException(String message) { 
        super(message); 
    } 
    
  2. 할 수 있습니다하지 returnthrow 후, 즉시에 점프 이후를 가장 가까운 catch 블록입니다.이 경우에는 메서드 외부에 있습니다. 에

+0

감사! 그것은 효과가 있었다. – Palak

관련 문제