加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java异常处理的五个关键字

发布时间:2020-12-14 19:44:11 所属栏目:Java 来源:网络整理
导读:异常:异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。 异常处理关键字:try、catch、finally、throw、throws 注意事项: 错误不是异常,而是脱离程序员控制的问题。 所有的异常类是从 java.lang.Exception 类继承的

异常:异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。

异常处理关键字:try、catch、finally、throw、throws

注意事项:

  1. 错误不是异常,而是脱离程序员控制的问题。
  2. 所有的异常类是从 java.lang.Exception 类继承的子类。
  3. 异常类有两个主要的子类:IOException 类和 RuntimeException 类。
  4. Java有很多的内置异常类。

异常大致分类:

  1. 用户输入了非法数据。
  2. 要打开的文件不存在。
  3. 网络通信时连接中断,或者JVM内存溢出。

语法:

try{
//需要监听的代码块
}
catch(异常类型 异常名称/e){
//对捕获到try监听到的出错的代码块进行处理
throw 异常名称/e; //thorw表示抛出异常
throw new 异常类型(“自定义”);
}

finally{
//finally块里的语句不管异常是否出现,都会被执行
}
修饰符 返回值 方法名 () throws 异常类型{ //throws只是用来声明异常,是否抛出由方法调用者决定
//代码块
}

代码例子:(try与catch与finally)

public class ExceptionTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in); 
    try{ //监听代码块 
    int a=input.nextInt(); 
    int b=input.nextInt(); 
    double sum=a/b;  
    System.out.println(sum); 
    } 
    catch(InputMismatchException e){ 
      System.out.println("只能输入数字"); 
    } 
    catch(ArithmeticException e){ 
      System.out.println("分母不能为0"); 
    } 
    catch(Exception e){ //Exception是所有异常的父类 
      System.out.println("发生了其他异常"); 
    } 
    finally{ //不管是否出现异常,finally一定会被执行 
      System.out.println("程序结束"); 
    } 
	}
}

代码例子:(throw关键字)

import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in); 
    try{ //监听代码块 
    int a=input.nextInt(); 
    int b=input.nextInt(); 
    double sum=a/b;  
    System.out.println(sum); 
    } 
    catch(InputMismatchException e){ //catch(异常类型 异常名称) 
      System.out.println("只能输入数字"); 
      throw e; //抛出catch捕捉到的异常 
      //throw new InputMismatchException(); 同上 
    } 
    catch(ArithmeticException e){ 
      System.out.println("分母不能为0"); 
      throw new ArithmeticException("分母为0抛出异常"); //抛出ArithmeticException异常 
    } 
    catch(Exception e){ //Exception是所有异常的父类 
      System.out.println("发生了其他异常"); 
    } 
    finally{ //不管是否出现异常,finally一定会被执行 
      System.out.println("程序结束"); 
    }  
	}
}

代码例子:(throws)

public class Throws {
	int a=1;
	int b=0;
	public void out() throws ArithmeticException{ //声明可能要抛出的异常,可以有多个异常,逗号隔开
		try{ //监听代码块
		int sum=a/b;
		System.out.println(sum);
		}
		catch(ArithmeticException e){
			System.out.println("分母不能为0");
		}
		finally{ //不管是否出现异常,finally一定会被执行
			System.out.println("程序结束");
		}
	}
	public static void main(String[] args){
		Throws t=new Throws();
			t.out(); //调用方法
			throw new ArithmeticException("分母为0抛出异常"); //由调用的方法决定是否要抛出异常
			/*
			 * 第二种抛出方式
			 */
//			ArithmeticException a=new ArithmeticException("分母为0抛出异常");
//			throw a;
	}
}

您可能感兴趣的文章:

  • Java异常跟踪栈定义与用法示例
  • Java异常处理学习心得
  • Java异常基础知识解析
  • 浅析java异常栈
  • JAVA异常体系结构详解
  • Java异常区分和处理的一些经验分享
  • java异常继承何类,运行时异常与一般异常的区别(详解)
  • Java异常 Exception类及其子类(实例讲解)
  • Java异常退出条件的判断示例代码
  • 深入分析Java异常

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读