异常捕获 try catch
加上异常捕获
如果不打印异常信息
============================ ============================ ============================ ============================ ============================
关于多层异常嵌套捕捉
package com.zyq.test1; import com.sun.media.jfxmedia.logging.Logger; import org.apache.poi.ss.formula.functions.T; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { try { System.out.println("今天的天气很冷呀!!!"); } catch (Exception e) { System.err.println("错误:"+e); } finally { try { int a=1/0; } catch (Exception ex) { System.err.println("出错啦!"+ex); } } } }
运行结果:
今天的天气很冷呀!!!
出错啦!java.lang.ArithmeticException: / by zero
package com.zyq.test1; import com.sun.media.jfxmedia.logging.Logger; import org.apache.poi.ss.formula.functions.T; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { try { System.out.println("今天的天气很冷呀!!!"); int b=1/0; } catch (Exception e) { System.err.println("错误:"+e); } finally { try { int a=1/0; } catch (Exception ex) { System.err.println("出错啦!"+ex); } } } }
运行结果:
今天的天气很冷呀!!!
错误:java.lang.ArithmeticException: / by zero
出错啦!java.lang.ArithmeticException: / by zero