Java基础-异常类--异常概念、一般格式、Throwable类的一些方法
简介:
1、异常的概念
异常:问题-->不正常情况---》封装成对象 ;
java对不同的不正常情况进行描述后的对象体现;
对于严重的异常,java通过Error类进行描述-------一般不编写针对性的代码对其进行处理
对于非严重可处理的异常的,java通过Exception类进行描述--可使用针对性的代码进行处理
体系: Object<--Throwable<--Error/Exception
2、一般格式
try{ * * }
catch(异常类 变量){ * 处理问题* }
finally{ * 一定会执行的语句; * }
3、Throwable类的一些方法
Returns the detail message string of this throwable.
void printStackTrace()
Prints this throwable and its backtrace to the standard error stream.
Returns a short description of this throwable.
ExceptionTest.java
public class ExceptionTest {
    public static void main(String[] args){
        //检测
        try{
            int resultD = DivDemo.div(10, 0);
            System.out.println("resultD'value is :"+resultD);
        }
        //接收截获异常对象,然后处理问题,处理完后往下执行。
        catch(Exception e){        
            System.out.println("Get it: "+e.getMessage());
            System.out.println("Get it: "+e.toString());
            e.printStackTrace();  
//打印三个信息----异常名称,异常信息,异常出现的位置
            //其实jvm默认的异常处理机制:就是在调用printStackTrace()方法
            //打印异常的堆栈的跟踪信息
        }
        
        System.out.println("over!");
    }
}
class DivDemo{
    
    public static int div(int a,int b){
        return a/b;   
    }
}
:console:
                    
                


                
            
        
浙公网安备 33010602011771号