java面向对象总结异常

 1-5在往期

6.异常

6.1异常种类

checked异常,编译时异常,必须在编写的时候处理

runtime异常,运行期异常,不是在编写时必须处理

6.2异常关键字

try         catch         finally         throw         throws

try catch finally:

import java.util.Scanner;



public class Demo1 {

	int a = 0;



	public static void main(String[] args) {

		try {

			Scanner sc = new Scanner(System.in);// 导包快捷键CTRL+shift+o

			System.out.println("请输入被除数");

			int num1 = sc.nextInt();

			System.out.println("请输入除数");

			int num2 = sc.nextInt();

			System.out.println(num1 + "/" + num2 + "=" + num1 / num2);

			System.out.println("感谢使用本程序");

			// catch() 方法中的异常代表捕获的异常

		}catch (ArithmeticException e) {
//异常从小到大,排列


			// 处理异常的一个解决方案

			System.err.println("除数为0了,不能为0");

			e.printStackTrace();

		} catch (Exception e) {
// 处理异常的一个解决方案


			System.err.println("除数为0了,不能为0");

			e.printStackTrace();

		}finally {
//无论有无异常,都会执行

			System.out.println("感谢使用本程序");

		}
try catch捕获异常后,会新开辟空间,执行异常之后的语句


		System.out.println("hehehe");

	}

}

throw throws:

public void print() throws Exception {

	throw new Exception("haha");

}

6.3常见异常类型

算数异常:ArithmeticException(分母为0,对数的底数为0,对数的指数为负)

输入不匹配:InputMismatchException

类型转换异常:ClassCastException

数组下标越界异常:ArrayIndexOutOfBoundsException

空指针异常:NullPointerException

找不到指定类异常:ClassNotFoundException

非法参数异常:illegaArgumentException

数值格式转换异常:NumberFormatException

下标越界异常:IndexOutOfBoundsException

输入输出异常:IOException

数据库异常:SqlExcepyion

eclipes快捷键:

ctrl+shift+o 导包快捷键

alt+shift+z 异常try快捷键

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值