《Java基础——异常的捕获与抛出》

Java基础——异常的捕获与抛出

        

        

        

前言:

  1. Error类(错误)和Exception类(异常)是Throwable类的子类。
  2. 异常分为CheckedException类(编译时异常)和RuntimeException类(运行时异常)。
  3. CheckedException类(编译时异常)必须提前处理。
  4. RuntimeException类(运行时异常)可选择性处理

        

一、捕获异常;


规则:

  1. try()里的代码A或代码B只要有一个出现异常就会运行catch()里的代码C。
  2. 无论是否有异常都会运行finally里的代码D。
  3. 假如要捕获多个异常,要从小到大。

        

格式:

try
{  
	待捕获代码A
    待捕获代码B
}  
catch(想要捕获的异常类型 对象名)
{  
	异常被捕获时的输出代码C  
}  
finally
{
    代码D
}
后续代码

例如:

int m=2, n=0;
try
{
    System.out.println(m/n);
}
catch(Exception e)
{
    System.out.println("除数不能为0");
}
catch(Throwable t)
{
    System.out.println("Throwable");
}
finally
{
    System.out.println("finally");
}

编译结果:

除数不能为0
finally

        

        

二、抛出异常;


规则:

  1. 当判断方法存在异常时先通过关键字throws抛出方法异常。
  2. 然后可以选择在主函数中选择捕获异常或继续抛出异常。

        

例一:捕获异常

public static void main(String[] args)
{
	try 
    {
    	int a=num(4,0);
        System.out.println(a);
    }
    catch(Exception e)
    {
        e.printStackTrace();                      //在命令行打印异常信息在程序中出错的位置及原因。
        System.out.println("除数不能为0");
    }      
}
public static int num(int x,int y)throws Exception
{
    int result = x/y;
    return result;
}

编译结果:

java.lang.ArithmeticException: / by zero
	at 源代码.Example.num(Example.java:22)
	at 源代码.Example.main(Example.java:10)
除数不能为0

        

例二:双重抛出异常

public static void main(String[] args)throws Exception
{
	num();
}
public static void num()throws Exception
{
	int x=4, y=0;
	int m=x/y;
	System.out.println(m);
}

编译结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero

        

例三:主动双重抛出异常

public static void main(String[] args)throws Exception
{
	num();
}
public static void num()throws Exception
{
	int x=4, y=0;
	if(y==0)
	{
		throw new Exception("除数不能为0");
	}
	int m=x/y;
	System.out.println(m);
}

编译结果:

Exception in thread "main" java.lang.Exception: 除数不能为0

        

        

三、自定义异常;


规则:

  1. 定义自定义异常类,继承至Exception类,重写其中的两个构造方法。
  2. 在异常所在方法的后面添加throws 语句,表示该方法有一个自定义异常类型的异常信息需要处理。
  3. 通过if语句设置条件,确定需要抛出异常的位置,使用throw 语句抛出自定义异常类的实例。

        

格式:

class 自定义异常名 extends Exception
{
    public 自定义异常名()
    {

    }
    public 自定义异常名(String msg)
    {
        super(msg);
    }
}

例如:

public class Student 
{
		public static void main(String[] args)throws MyException
	    {
			String username ="巴提";
	        if(username.length()<3)
	        {
	            throw new MyException("用户名小于三位");
	        }
	        System.out.println("用户名格式正确");		
		} 
}
class MyException extends Exception
{
    public MyException()
    {

    }
    public MyException(String msg)
    {
        super(msg);
    }
}

编译结果:

源代码.MyException: 用户名小于三位
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温稚生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值