package test.s;
public class yichang {
public static void main(String[] args) throws Exception{
try{
double a=aa();
System.out.println(a);
}catch(Exception e){
e.printStackTrace();
}
}
public static double aa() throws Exception{
double b = 0;
try{
b=1/0;
}catch(Exception e){
throw new Exception(e.getMessage());
}
return b;
}
}
java.lang.Exception: / by zero
at test.s.yichang.aa(yichang.java:18)
at test.s.yichang.main(yichang.java:6)
说明:这算是比较正常的异常写法。aa()方法抛出异常,mian方法捕获异常,并打印出异常原因。
2,
package test.s;
public class yichang {
public static void main(String[] args) throws Exception{
try{
double a=aa();
System.out.println(a);
}catch(Exception e){
}
}
public static double aa() throws Exception{
double b = 0;
try{
b=1/0;
}catch(Exception e){
throw new Exception(e.getMessage());
}
return b;
}
}
没有输出;
说明:这个跟1的区别是main方法捕获aa传来的异常后没有将异常打印出来,所以没有任何输出。
3,
package test.s;
public class yichang {
public static void main(String[] args) throws Exception{
try{
double a=aa();
System.out.println(a);
}catch(NullPointerException e){
e.printStackTrace();
}
}
public static double aa() throws Exception{
double b = 0;
try{
b=1/0;
}catch(Exception e){
throw new Exception(e.getMessage());
}
return b;
}
}
输出:
Exception in thread "main" java.lang.Exception: / by zero
at test.s.yichang.aa(yichang.java:18)
at test.s.yichang.main(yichang.java:6)
效果跟下面的代码是一样的:也就是main方法中不用try catch
package test.s;
public class yichang {
public static void main(String[] args) throws Exception{
double a=aa();
System.out.println(a);
}
public static double aa() throws Exception{
double b = 0;
try{
b=1/0;
}catch(Exception e){
throw new Exception(e.getMessage());
}
return b;
}
}
package test.s;
public class yichang {
public static void main(String[] args) throws Exception{
try{
double a=aa();
System.out.println(a);
}catch(NullPointerException e){
e.printStackTrace();
}
}
public static double aa() throws Exception{
double b = 0;
try{
b=1/0;
}catch(NullPointerException e){
throw new NullPointerException(e.getMessage());
}
return b;
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.s.yichang.aa(yichang.java:16)
at test.s.yichang.main(yichang.java:6)
因此同以下代码:
package test.s;
public class yichang {
public static void main(String[] args){
double a=aa();
System.out.println(a);
}
public static double aa() {
double b = 0;
b=1/0;
return b;
}
}
package test.s;
public class yichang {
public static void main(String[] args) throws Exception{
try{
double a=aa();
System.out.println(a);
}catch(NullPointerException e){
e.printStackTrace();
}
}
public static double aa() throws Exception{
double b = 0;
try{
b=1/0;
}catch(ArithmeticException e){
throw new ArithmeticException(e.getMessage());
}
return b;
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.s.yichang.aa(yichang.java:18)
at test.s.yichang.main(yichang.java:6)
6,最准确的情况
package test.s;
public class yichang {
public static void main(String[] args) throws Exception{
try{
double a=aa();
System.out.println(a);
}catch(ArithmeticException e){
e.printStackTrace();
}
}
public static double aa() throws Exception{
double b = 0;
try{
b=1/0;
}catch(ArithmeticException e){
throw new ArithmeticException(e.getMessage());
}
return b;
}
}
输出:
java.lang.ArithmeticException: / by zero
at test.s.yichang.aa(yichang.java:18)
at test.s.yichang.main(yichang.java:6)
总结,正确使用try catch 异常,try 不是能吃掉所有的异常,必须要在catch中使用正确的异常才能捕获。但是在实际开发中,很难精确的捕获可能存在的异常。因此我们大多使用第一种情况,exception是所有异常的父类,能捕获到所有的异常。
新增:对于方法套嵌层级很多的,如果在最外层的方法被try catch,那么无论多少层级,最后都会被最外层的try catch捕获到,比如说在实际工作中我们经常会看到这样的代码,最外层的方法被try catch,如果有个方法出现空指针异常,那么最后打印的信息会是最外层catch输出的错误说明。