zoukankan      html  css  js  c++  java
  • RuntimeException异常

    /*
    Exception有一个特殊的子类异常RuntimeException运行异常,如果在函数内部抛出此异常,函数上可以不用声明异常,编译一样通过。
    如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。之所以不在函数声明,是因为不要调用者处理,当
    该异常出现,程序会停止,因为在运行时,出现了无法运算的异常,希望程序停止后,希望程序员对代码进行修改。
    
    自定义异常时,如果该异常的发生让程序无法继续运行,就让自定义异常继承RuntimeException类,有许多它的子类就没有在函数声明
    异常分两种:
    1、编译时检测的异常,
    2、编译时不被检测的异常(RuntimeException异常及其子类异常)
    */
    
    class RuntimeDemo
    {
    	int div(int a,int b)
    	{
    		if(b==0)
    			throw new ArithmeticException("除数为0");			
    			//throw new Exception("除数为0");   此时编译会失败。此时要在函数后进行throws ArithmeticException声明。
    			//声明就是告诉调用者有问题,要处理
    		return a/b;
    	}
    }
    
    class Exception
    {
    }
    class  RuntimeExceptionDemo
    {
    	public static void main(String[] args) 
    	{
    		RuntimeDemo rd=new RuntimeDemo();
    		int x=rd.div(4,0);
    		System.out.println("x="+x);
    		System.out.println("Over");
    	}
    }
    

  • 相关阅读:
    作业16
    递归函数
    三元表达式与生成式
    迭代器与生成器
    作业15
    装饰器
    作业14
    string的入门与进阶
    修理牧场(优先队列)
    旅游规划
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258557.html
Copyright © 2011-2022 走看看