我的异常博客,自定义异常,异常的分类

本文深入探讨Java中的异常处理机制,包括常见的异常类型如NullPointerException、ArrayIndexOutOfBoundsException等,以及如何使用try-catch-finally结构进行有效的错误捕捉和处理。同时,通过实例演示自定义异常的创建与抛出,帮助读者理解异常的继承关系和处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常:

Throwable

  • Error
    • OutOfMemoryError
  • Exception
    • RuntimeException
      • ClassCastException
      • NullPointerException
      • IndexOutOfBoundsException
      • ArithmeticException
    • (CheckedException)
      • IOException
      • SqlException
try-catch-finally
  • try
  • catch
    • 可以有多个
    • 异常与异常之间的关系
      • 没有关系 多个catch块
      • 继承关系 子类的catch块放在前面(向上造型)
      • 处理方式相同: ClassCastException | IOException
  • finally
  • try块必须,其他至少二选一

异常的图片

执行代码与我的注解

import java.math.BigDecimal;

public class TryCatchPractice {
public static void main(String[] args) {
	char a[] = new char[1];
	String str=null;
	try {
		System.out.println("trycatch练习");
		a[7] = 0;
		//数组越界
		BigDecimal errorbigDecimal = new BigDecimal(3 / 0);
		System.out.println(errorbigDecimal);
		//算数异常
		System.out.println(str.charAt(7));
		//空指针异常		
		
//当程序执行到运行时异常时,不管后面还有多少没有执行的语句,都不会继续执行,转为执行catch语句块,但finally必须执行
      
	}
  catch (NullPointerException e) {
		System.out.println("运行时异常zhi空指针异常");
		//空指针一般存在获取的字符串为空时,找不到目标的存在
	} 
  catch (ArithmeticException e) {
		System.out.println("运行时异常zhi算数异常");
		//算数异常最简单的例子时,除数不能为0,如果为0 ,执行这个异常
	} 
  catch (ArrayIndexOutOfBoundsException e) {
		System.out.println("运行时异常zhi数组越界");
		//越界:当获取 的值取出的下标,并不在选择的范围,就执行这个异常

	} 
  catch (Exception e) {
		System.out.println("exception异常捕获");
		//exception一定的catch一定要最后写,从上往下依次解决,exception异常是这些异常的父类,只能最后写
	}
	finally {
		System.out.println("我一定要运行");
	}
	
	System.out.println("这是try—catch语句块外的语句");
}}

执行结果:
trycatch练习
运行时异常zhi数组越界
我一定要运行
这是try—catch语句块外的语句

注意:return 时,finally任然执行;system.exit(0)时,finally 不执行

自定义异常:
Test类

public class Test {
public static void main(String[] args) {
	PayMoney payMoney=new PayMoney();
	payMoney.payfor(100, "张三");
}
}

支付金额类:PayMoney

public class PayMoney {
public void payfor(double money, String username) {

		User user = new User();
		user.setPrice(10000);
		user.setUsername(username);
		if (money>user.getPrice()) {
			System.out.println("恭喜您,你花了"+user.getPrice()+"元,买了一个岛");
			System.out.println("您的余额为:"+BigDecimal.valueOf(money).subtract(BigDecimal.valueOf(user.getPrice()))+"元");
		}else {
			try {
				throw new CustomRunTimeException("提示: 你输入的金额不够", "去你的,钱都不够,买啥岛        (▼⊿▼)");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("解决办法:买岛,钱不够(^_^)");
			}
		}
		
	}

}

继承runtimeException

public class CustomRunTimeException extends RuntimeException {

	private String currentString;

	public String getCurrentString() {
		return currentString;
	}

	public void setCurrentString(String currentString) {
		this.currentString = currentString;
		
	}

	public CustomRunTimeException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public CustomRunTimeException(String message, Throwable cause, boolean enableSuppression,
			boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}

	public CustomRunTimeException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public CustomRunTimeException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	/*
	 * 我自定义的异常("抛出异常的内容,抛出异常的原因")
	 */
	public CustomRunTimeException(String message, String name) {
		super(message);
		this.setCurrentString(name);
		System.out.println("客服提示:"+name);
		//保证 值传到异常这个地方

	}

	public CustomRunTimeException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}
}

执行结果:
在这里插入图片描述
注意:抛出runtime异常,可以在编译期不管他,检查不会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值