MichaelBlog

double i = Double.MAX_VALUE; while(i == i + 1){ System.out.print ("学无止境");};

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Java异常处理

Java异常处理

作用: 使程序可以处理非预期的错误,并且继续正常处理。

异常是从方法抛出的,方法调用者可以捕获并处理该异常

语法:


throw 抛出...



try{
	... //正常语句
}
catch(){ //捕获
	...
}

例子:

public class QuotientWithException { 
  public static int quotient(int number1, int number2) {
    if (number2 == 0) //商不可以为0
      throw new ArithmeticException("Divisor cannot be zero");//抛出一个异常
    return number1 / number2;
  }
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    // Prompt the user to enter two integers
    System.out.print("Enter two integers: ");
    int number1 = input.nextInt();
    int number2 = input.nextInt();
    
    try {                                 
      int result = quotient(number1, number2);
      System.out.println(number1 + " / " + number2 + " is " 
        + result);//正常结果
    }
    catch (ArithmeticException ex) {  //处理异常 ArithmeticException
      System.out.println("Exception: an integer " + 
        "cannot be divided by zero ");//异常结果
    }

    System.out.println("Execution continues ...");
  }
}

public class InputMismatchExceptionDemo {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    boolean continueInput = true;

    do {
      try {
        System.out.print("Enter an integer: ");
        int number = input.nextInt();
  		//输入不是整数 InputMismatchException异常
        // Display the result
        System.out.println(
          "The number entered is " + number);
        
        continueInput = false;
      } 
      catch (InputMismatchException ex) {
        System.out.println("Try again. (" + 
          "Incorrect input: an integer is required)");
        input.nextLine(); // 重新输入 
      }
    } while (continueInput);
    //continueInput = false;
  }
}

java异常类继承关系图
java异常类继承关系图

More…

Java异常处理模型

1.声明一个异常(declaring an exception)
	方法要抛出异常必须在方法头中显式声明,
	方法调用者 会被告知有异常。
Public void method() throws IOException

//多个异常
public void myMethod()
	throws Excepton1,Excepton2,...ExceptonN
2.抛出一个异常(throwing an exception
throw new IllegalArgumentException("Wrong Argument")

🎈注意:声明是throws,抛出是throw
3.捕获一个异常(catching an exception)

try-catch多异常处理
try{
	statements;
}
catch(Exception1 exVar1){
	handler for exception1;
}
catch(Exception1 exVar2){
	handler for exception2;
}
...
catch(Exception1 exVarN){
	handler for exceptionN;
}
//如果在执行try块的过程中没有出现异常,则跳过catch子句。

🎈注意:若一个catch块可以捕获父类的异常对象,它就能捕获父类的所有子类的异常对象。
eg:

try{
...
}
catch(RuntimeException ex){
...
}
catch(Exception ex){
...}

posted on   Michael_chemic  阅读(33)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示