2013-03-05 3 views
0

먼저 다음을 읽어야합니다. http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html! 내가 오류 메시지가 나에게 힌트의 몇 가지 유형을주고 있으리라 믿고있어throwable가 컴파일되지 않음

TestException.java:14: error: incompatible types 

static void doRisky(String test) throws ScaryException 
               ^
required: Throwable 
found: ScaryException 

TestException.java:19: error: incompatible types 
         throw new ScaryException(); 
            ^
required: Throwable 
found: ScaryException 

TestException.java:34: error: incompatible types 
       catch (ScaryException se) 
         ^
    required: Throwable 
    found: ScaryException 

3 errors 

,하지만 난 그냥이 무엇인지 표시되지 않습니다 :

내가 컴파일 할 때 내가 오류는 다음과 .

import java.lang.*; 
/** add this as to get it to compile */ 
class ScaryException extends Exception 
{ 
/** how come I can't put code here? */ 
} 

public class TestException { 

    static void doRisky(String test) throws ScaryException{ 
     System.out.println ("start risky");  
     if ("yes".equals(test)) { 
      throw new ScaryException(); 
     } 
     System.out.println("end risky"); 
    } 

    public static void main(String [] args){ 
     String test = "no"; 
     try{ 
      System.out.println("start try"); 
      doRisky(test); 
      doRisky("yes"); 
      System.out.println("end try"); 
     } 
     catch (ScaryException se){ 
      System.out.println("Got What " + se); 
     } 
     finally{ 
      System. out. println("finally") ; 
     } 

     System.out.println("end of main"); 
    } 
} 
+0

'ScaryException'가 throwable의 서브 클래스를 확장합니까? 보통'java.lang.Exception'인가? – PermGenError

답변

2

자바 예외 (Exception 또는 하위 클래스 중 하나를 확장하여 직접, 간접적으로 또는 바람직하게는) Throwable 연장한다. 분명히 ScaryException 클래스는 그렇지 않습니다.

+0

그래, 어떻게 고칠 수 있니? – user1984948

+0

가장 쉬운 방법은 'extends Exception'을 'ScaryException'의 정의에 추가하는 것입니다 (예 : class ScaryException extends Exception'). 물론 예외 처리를 재 작성하는 것과 같이 별 효과가없는 뭔가를 처리했다고 가정합니다. :) – cHao

1

컴파일하려면 ScaryExceptionThrowable 또는 그 서브 클래스 중 하나를 확장하는 클래스 여야합니다. ScaryException이 전혀 존재하지 않거나 Throwable의 하위 항목을 확장하지 않습니다.