你好

异常

 

   

Error

  类对象由jvm生成并抛出,一般与程序本身无关,一般会自动终止程序运行,如栈溢出

Exception

  •   RuntimeException,是不检查异常,可以选择捕获处理或不捕获。一般由程序逻辑错误引起
  •        非运行时异常

 

 

快速生成异常处理:选中一行代码,快捷键Ctrl+Alt+T  

 1 public class t {
 2 
 3     public static void main(String[] args) {
 4         try {
 5             new t().a();  //调用本类的方法
 6         } catch (Error e) {      //先进行小范围的捕获
 7             System.out.println("发现异常1");
 8         }catch (Throwable e)   //再扩大捕获范围
 9         {
10             System.out.println("发现异常2");
11         }
12 
13     }
14   public void a()
15     {
16         b();
17     }
18   public void b()
19     {
20         a();
21     }
22 }
 1    public static void main(String[] args) {
 2         try {                //捕获接收到的异常并处理
 3             new t().test();
 4         } catch (Error error) {
 5             System.out.println("发现异常");
 6         }
 7         System.out.println("finish");
 8     }
 9 
10     void test() throws Error    //方法内处理不了时向上层抛出
11     {
12         a();
13     }
14   public void a()
15     {
16         b();
17     }
18   public void b()
19     {
20         a();
21     }                                                                                                                
上层抛出异常

 

  

posted @ 2020-10-02 15:12  S_nA_tCH!  阅读(51)  评论(0)    收藏  举报