Why doesn't catching Exception catch RuntimeException?

后端 未结 5 735
眼角桃花
眼角桃花 2020-12-08 00:05

This is very odd to me. RuntimeException inherits from Exception, which inherits from Throwable.

catch(Exception exc)          


        
5条回答
  •  青春惊慌失措
    2020-12-08 00:39

    class Test extends Thread
    {
        public void run(){  
            try{  
                Thread.sleep(10000);  
            }catch(InterruptedException e){  
                System.out.println("test1");
                throw new RuntimeException("Thread interrupted..."+e);  
            }  
    
        }  
    
        public static void main(String args[]){  
            Test t1=new Test1();  
            t1.start();  
            try{  
                t1.interrupt();  
            }catch(Exception e){
                System.out.println("test2");
                System.out.println("Exception handled "+e);
            }  
    
        }  
    }
    

    Its output doesn't contain test2 , so its not handling runtime exception. @jon skeet, @Jan Zyka

提交回复
热议问题