Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 2 decades ago

Interface 可以丟出例外嗎?

Interface 可以丟出例外嗎?

突然忘記了 有誰可以提醒一下的嗎

5 Answers

Rating
  • Anonymous
    2 decades ago
    Favorite Answer

    不可以。

    只有 Throwable 或 Throwable 的 subclasses 才可以丟出。

    ------------------------------------------------------------------------------------

    Ref :

    The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

  • Anonymous
    2 decades ago

    =.=||

    好久沒來了...

    來了看到這個...

    冷了好久...

    總覺得有點怪怪的~

    不懂Java的人來問問題~

    確由不懂Java的人(發問者)來選答案~

    假設有人亂貼答案的話!

    那麼...發問的人怎麼知道哪個是正確的??

  • Lv 7
    2 decades ago

    Pon,一個講可以.一個講不可以.做遊戲的人講的應才是正確的

  • Anonymous
    2 decades ago

    你們兩個都講的很詳細 謝謝

    2005-07-07 17:20:30 補充:

    是喔 小弟愚昧... = =

  • How do you think about the answers? You can sign in to vote the answer.
  • 2 decades ago

    如果是說Interface裡的method可不可以丟出例外的話, 答案是可以. 以下的程式是合法的.

    public interface TryThrowable

    {

    public void throwSomething() throws Exception;

    }

    public class TestMain implements TryThrowable

    {

    public void throwSomething() throws Exception {

    throw new Exception("Hehehe");

    }

    public static void main( String[] arg ) {

    try {

    new TestMain().throwSomething();

    }

    catch( Exception e ) {

    e.printStackTrace();

    }

    }

    }

    但是Interface本身不可以是例外. 因為所有例外都是由Throwable繼承來的, 而Throwable是一個Class.

    Source(s): JDK + J2SE API Doc
Still have questions? Get your answers by asking now.