JavaSE 异常

JavaSE 异常

1。异常的慨述和分类
  A.异常的慨述
异常就是java程序在运行过程中出现的错误
  B.异常的分类
通过API查看throwple
Error
   服务器启机。数据库奔溃等
exception
  C:异常的继承体系
throwable
error
  exception
 runtimeException
2。JVM默认是如何处理异常的
 A.JVM默认是如何处理异常的
main函数收到这个问题时。有两种处理方式:
a.自己将该问题处理。然后继续运行
b.自己没有针对的处理方式、只有交给main的jvm来处理
JVM有一个默认的处理方式。就将该异常进行处理
并将该异常的名称。异常的信息。异常出现的位置打印到控制台上。同时将程序停词运行
B.案例演示:
jvm默认如何处理异常

public static void main(String[] args){
      try{
          System.out.println(1/0);
        ) catch (Exception e) {            //Excption e = new ArithmeticException("/ by zero")
        //System.out.println(e.getMessage());      //获取异常信息
        System.out.println(e.toString());    //调用tostring方法,打印异常类名和异常信息。
        e.printstackTrace();    //jvm默认就这种方式处理异常
        }
      }
  }

public static void main(String[] args){
     //demol();
     Demo d = new Demo();
     int x = d.div(10, 0);
     System.out.println(x);
 }
  public static void main(){
      int[] arr = (11.22.33.44.55);
      //arrr = null;        //NullpointerException        空指针异常
      System.out.println(arr[10]);        //ArraIndexOutofBoundsException数组牵引越界异常
  }
 }
 class Demo{
     /*
     ×chu法运算
     */
     public int div(int a.int b) {        //a = 10,b = 0
         return a / b;        /// 10 / 0 当被除数是10,除数是0时违背了算数运算法则抛出异常。
         //new ArithneticException("/ by zero")
         
     }
 }

3.异常(try。。。catch的方式处理异常1)
1.异常处理的两种方式
try…catch…finally
try catch
try catch fially
try finally
throws
try…catch异常处理的基本格式
try…catch…finally
案例演示
try…catch的方式处理1个异常
try;用了检查异常
catch;用了捕获异常
finally;释放资源

public static void main(String[] args){
Demo2 d = new Demo2();
try{
int x = d.div(10, 0 );
Syste.out.println(x);
}catch(ArithmeticException a) { //ArithmeticException a = new ArithmeticException();
System.out.println(“出错了。除数为令了”);//只有try出问题了才会找catch
}
System.out.println(“11111111111”)
}

class Demo2{
     /*
     ×chu法运算
     */
     public int div(int a.int b) {        //a = 10,b = 0
         return a / b;    

public static void main(String[] args) {
    try{
        System.out.println(1/0);
    } catch (Exception e){   //Exception e = new ArithmeticException("/ by zero")
    //System.out.println(e.getMessage());    //获取异常信息
    //System.out.println(e);   //调用toString方法。打印异常类名和异常信息
    e.printStackTrace();    //jvm末日就用这种方式处理异常
    }
}

Finally关键字事务面试题
面试题1:
final.finally和finalize的区别
面试题2:
如果catch里面return有语句,请问finallly的代码还会执行吗?如果会.请问是在return前还是在return后。
public static void main(String[] args) {
}
}
class Demo {
public int method() {
int x = 10;
try {
x = 20;
System,out,println(1/0);
//return x;
} cath (Exception e){
x = 30;
//return x;
return x;
} finally{
x = 40;
//return x; 千万不finally要在里面写返回语,finally因为的作用是为了释放资源,是肯定
//如果在这里写返回语句,那么try和catch的结果就会被改变
}
}
}

练习

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入一个整数")
    
    while(true) {
            String line = Integer.parseInt();   //将键盘录入的结果存储在line中
        try{
            int num = Integer.parseInt(line);   //将字符串转换为整数
            System.out.println(Integer.toBinaryString(num));  //将整数转换为二进制
            break;                //跳出循环
        }catch(Exception e) {
            new BigInteger(line);
            System.out.println("录入错误,你录入的是一个过大整数,请从新输入一个整数");
        }catch (Exception e2) {  //alt + shif + z (try catch快捷建)
            tey{ new BigDecimal(line);
            System.out.println("录入错误,你录入的是一个过大整数,请从新输入一个整数");
            }catch (Exception e1) {
                System.out.println("录入错误,你录入的是一个过大整数,请从新输入一个整数")
    }
}

猜你喜欢

转载自blog.csdn.net/A2307812087/article/details/83218116