Java Exception as checked Exception but not required to be thrown in trycatch

前端 未结 2 524
半阙折子戏
半阙折子戏 2021-01-20 00:37

I have this snippet.

public final class StackOverflow{
   class MyException extends Throwable{
   }
   private void a(){
       try{
       }catch(MyExceptio         


        
2条回答
  •  無奈伤痛
    2021-01-20 00:53

    Exception extends from RuntimeException will considered as uncheched exception, so it's ok:

    class MyException extends RuntimeException { }
    
    try {
        ...
    } catch (MyException e) {
    
    }
    

    Your exception extends from Throwable, so it is cheched exception. Since the compiler noticed that it is never thrown, so the compile fails.

提交回复
热议问题