软件编程
位置:首页>> 软件编程>> java编程>> Java深入讲解异常处理try catch的使用

Java深入讲解异常处理try catch的使用

作者:Demo龙  发布时间:2023-11-04 13:00:32 

标签:Java,try,catch,异常处理

1.try-catch异常处理说明

Java提供try和catch块来处理异常,try块用于包含可能出错的代码。catch块用于处理try块中发出的异常。可以根据需要在程序中有一个或多个try-catch块

基本语法

try{
//可疑代码
//将一场生成对应的异常对象,传递给catch块
}catch{
//对异常的处理
}

2.try-catch异常处理细节

1.如果异常发生了,则异常后面的代码不会执行,直接进入到catch块

2.如果异常没有发生,则顺序执行try的代码块,不会进入到catch。

3.如果希望不管是否发生异常,都执行某段代码,则使用如下代码-finally{}

示例01

package com.demo.trycatch;

/**
* @version 1.0
* @auther Demo龙
*/
public class TrycatchDetial {
   public static void main(String[] args) {
       //1.如果异常发生了,则异常后面的代码不会执行,直接进入到catch块
       //ctrl+alt+t
       try {
           String ch="kkk";
           int a=Integer.parseInt(ch);
           System.out.println("number"+a);
       } catch (NumberFormatException e) {
           System.out.println("异常信息:"+e.getMessage());
       }
       System.out.println("程序继续运行");
       // > 2.如果异常没有发生,则顺序执行try的代码块,不会进入到catch。
       try {
           String ch="556";
           int a=Integer.parseInt(ch);
           System.out.println("number="+a);
       } catch (NumberFormatException e) {
           System.out.println("异常信息:"+e.getMessage());
       }
       System.out.println("程序继续运行02");
       //> 3.如果希望不管是否发生异常,都执行某段代码,则使用如下代码-finally{}
       try {
           String ch="Demo龙";
           int a=Integer.parseInt(ch);
           System.out.println("number="+a);
       } catch (NumberFormatException e) {
           System.out.println("异常信息:"+e.getMessage());
       } finally {
           System.out.println("finally被执行");
       }
   }
}

测试结果01

Java深入讲解异常处理try catch的使用

4.可以有多个catch语句,捕获不同的异常(进行不同的业务处理),要求父类异常在后,子类异常在前,如果发生异常,只会匹配一个catch。

5.try-finally配合使用,这种用法相当于没有捕获异常,程序会直接退出,但会执行finally业务逻辑

示例02

package com.demo.trycatch;
/**
* @version 1.0
* @auther Demo龙
*/
public class TrycatchDetial02 {
   //4.可以有多个catch语句,捕获不同的异常(进行不同的业务处理),
   // 要求父类异常在后,子类异常在前,如果发生异常,只会匹配一个catch。
   public static void main(String[] args) {
       try {
           Person person = new Person();
           person=null;
           System.out.println("name="+person.getName());//空指针异常
           int n1=9;
           int n2=0;
           int num=n1/n2;//数学异常
           //1.try代码块可能有多个异常
           //2.可以使用多个catch分别捕获不同的异常,相应处理
           //3.要求子类异常写在前面,父类异常写在后面
       }catch (NullPointerException e){
           System.out.println("空指针异常:"+e.getMessage());
       } catch (ArithmeticException c){
           System.out.println("算数异常:"+c.getMessage());
       } catch (Exception e) {
           System.out.println("异常情况:"+e.getMessage());
       } finally {
           System.out.println("finally代码块被执行");
       }
       System.out.println("程序继续运行。");
       //5.try-finally配合使用,这种用法相当于没有捕获异常,
       // 程序会直接退出,但会执行finally业务逻辑
       System.out.println("try-finally细节测试");
       try {
           int m1=9;
           int m2=3;
           System.out.println("m1/m2="+m1/m2);
       } finally {
           System.out.println("执行finally代码块02");
       }
       try {
           int m1=9;
           int m2=0;
           System.out.println("m1/m2="+m1/m2);
       } finally {
           System.out.println("执行finally代码块01");
       }
   }
}
class Person{
   private String name="demo龙";

public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
}

测试结果02

Java深入讲解异常处理try catch的使用

来源:https://zal321.blog.csdn.net/article/details/125011256

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com