Java学习--异常类RuntimeException面试技巧

在Java中,异常就是程序在运行时出现不正常的情况。在异常中,它们的父类是Throwable,它有两个子类Error和Exception,通常我们在程序中说的异常 情况是指Exception类,在这个Exception类中,有一个RuntimeException特殊的子类异常,如果在函数内有抛出此类或其子类的异常,此时在函数上不需要声明,编译一样可以通过。同 时如果在函数上声明了该异常,调用者可以不用进行处理,编译一样可以 通过。
下面举例说明:
例一:
class ExceptionDemo2
{
	public static void main(String[] args) 
	{
		
			Demo d=new Demo();
			int a=d.div(4,0);
			
			System.out.println(a);
			
			System.out.println("over");
		
	}

}

class Demo
{
	int div(int a,int b) throws Exception
	{	
		if (b==0)
			throw new ArithmeticException();
		return a/b;
	}
}
此例中,类Demo中有抛出异常,如果不处理,编译就会报以下错误:

ExceptionDemo2.java:7: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出...

例二:

class ExceptionDemo3
{
	public static void main(String[] args) 
	{
		
			Demo d=new Demo();
			int a=d.div(4,0);
			
			System.out.println(a);
			
			System.out.println("over");
		
	}

}

class Demo
{
	int div(int a,int b) throws ArithmeticException
	{	
		if (b==0)
			throw new ArithmeticException();
		return a/b;
	}
}
此例中,因为ArithmeticException是RuntimeException的子类,所以即使使用了throws ArithmeticException,将异常抛出,即使在主函数中没有做相应的处理,编译也会 通过,只是在调用时遇到除数为0的情况才会报出除数为0的异常,因为此种 情况是 需要程序员进行处理的 。
之所以不用在函数上声明,是因为不需要让调用者处理 。当该异常发生时,希望程序停止,因为在运行时,出现了无法继续运算的情况,希望停止程序后 ,有程序员对代码进行修正。

下面举一个自定义的异常类,继承自RuntimeException类,仅供参考学习。
class ExceptionDemo
{
	public static void main(String[] args) //throws Exception
	{
		//try
		//{
			Demo d=new Demo();
			int a=d.div(4,-1);
			
			System.out.println(a);
			
			System.out.println("over");
		
		//}	
/*		
		catch (ArithmeticException e)
		{
			System.out.println("Divided by zero!");
			System.out.println(e.getMessage());
			System.out.println(e.toString());
			e.printStackTrace();
			
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			System.out.println("Index out of bound!");
			
		}
		catch (FuShuException e)
		{
			System.out.println(e.toString());
			System.out.println("出现的负数是:"+ e.getNum());
			
		}
		*/
	
	
	}

}

//因为在Exception的父类throwable中已经定义了throwable(String message)构造函数,同时也有getMessage方法获得message,
//Exception中定义了Exception(Sting message)构造函数,可以用super(message)将参数传递给getMessage方法,同理下面自定义的异常也可以类似定义。
class  FuShuException extends RuntimeException //Exception
{
	private int num;
	
	FuShuException()
	{
		super();	
	}
	
	FuShuException(String message)
	{
		super(message);	
	}
	
	FuShuException(String message,int num)
	{
		super(message);	
		this.num=num;
	}
	
	public int getNum()
	{
		return num;
	}
	
}

class Demo
{
	int div(int a,int b) throws FuShuException,ArithmeticException,ArrayIndexOutOfBoundsException
	{	
		if (b<0)
			throw new FuShuException("除数中出现了负数,在此例中不允许 / fushu",b);
		//int[] arr=new int[a];
		else if (b==0)
			throw new ArithmeticException();
		
		//System.out.println(arr[a]);
		return a/b;
	}
}
此例中自定义了一个继承自RuntimeException的异常类FuShuException,同时在函数内抛出了异常,但是在主函数内并没有对相应的异常做处理,此时程序照样会编译通过。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值