Java 异常总结

Throwablede类是 Java 语言中所有错误或异常的超类。

两个子类的实例,Error 和 Exception
Error 是 Throwablede 的子类,用于指示合理的应用程序不应该试图捕获的严重问题。
Exceptionde 类及其子类是Throwablede 的一种形式,它指出了合理的应用程序想要捕获的条件。
 
package com.cwcec.test;
 
class FuShuException extends Exception
{
public FuShuException()
{
 
}
 
public FuShuException(String msg)
{
super(msg);
}
}
class Person
{
    int age;
    String name;
    public Person(String name,int age)
    {
     this.name = name;
     this.age = age;
    }
    
    public Person()
    {
    
    }
    
    public int method(int[] arr,int index) throws FuShuException  //throw是语句抛出一个异常。
    {
     if(arr == null)
     {
     throw new NullPointerException("数组不能为null");
     }
     if(index >= arr.length)
     {
     throw new ArrayIndexOutOfBoundsException("数组访问的下标越界");
     }
     if (index < 0)
     {
     throw new FuShuException("数组下标不能为负数");  //throws是方法可能抛出异常的声明
}
     return arr[index];
    }
}
 
public class FieldDemo 
{
public static void main(String[] args) throws FuShuException 
{ 
 
Person person = new Person("Tom", 50);
int[] arr = {1,2,3};
int rel = person.method(null, 2);
System.out.println(rel);
  
    }  
  
} 
 
异常的分类:
1、编译时被检测异常:只要是Exception和其子类,除了特殊子类RuntimeException体系。
      这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
       这种问题可以针对性的进行处理。
 
2、编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类。
      这种问题的发生无法让功能继续,运行无法进行,更多的是因为调用的原因导致的或者内部状态的
      改变而导致的。
      这种问题一般不处理,直接编译通过,在运行时,让调用者调用时程序强制停止。
 
所以自定义异常时,要么继承Exception,要么继承RuntimeException。
 
 throws 和throw的区别:
1、throws使用在函数上,是方法可能抛出异常的声明,
      throw使用在函数内,表示抛出一个异常;
2、throws抛出的是异常类,可以抛多个,用逗号隔开,
      throw抛出的是异常对象,只能有一个。
 
--------------------------------------------------------------------------------------------------------------------------------------
1. RuntimeException与Exception, Error不同点: 当方法体中抛出非RuntimeException(及其子类)时,方法名必须声明抛出的异常;但是当方法体中抛出RuntimeException(包括RuntimeException子类)时,方法名不必声明该可能被抛出的异常,即使声明了,JAVA程序在某个调用的地方,也不需要try-catch从句来处理异常。
class TestA{  
    //compiles fine.we don't need to claim the RuntimeException to be thrown here  
    void method(){  
        throw new RuntimeException();  
    }  
}  
class TestB{  
    void method() throws RuntimeException{  
        throw new RuntimeException();  
    }  
      
    void invokeMethod(){  
        //compiles fine. we don't need the try-catch clause here  
        method();  
    }  
}  
class TestC{  
      
    //compiles error.we need to claim the Exception to be thrown on the method name   
    void method(){  
        throw new Exception();  
    }  
}  
class TestD{  
    //compiles fine.  
    void method() throws Exception{  
        throw new Exception();  
    }  
} 

以下所有的相关异常的特性都不包括RuntimeException及其子类。

2. 假如一个方法在父类中没有声明抛出异常,那么,子类覆盖该方法的时候,不能声明异常。

class TestA{  
    void method(){}  
}  
class TestB extends TestA{  
      
    //complies error if the method overrided pertaining to the base class doesn't declare throwing exceptions  
    void method() throws Exception{  
        throw new Exception();  
    }  
} 
 
3. 假如一个方法在父类中声明了抛出异常,子类覆盖该方法的时候,要么不声明抛出异常,要么声明被抛出的异常继承自它所覆盖的父类中的方法抛出的异常。
class TestA{     
    void method() throws IOException{}     
}     
class TestB extends TestA{     
    //compiles fine if current method does not throw any exceptions     
    void method(){}     
}     
class TestC extends TestA{     
    //compiles fine because InterruptedIOException is inherited from IOException which is thrown by the overrided method of the base class     
    void method() throws InterruptedIOException{}     
}     
class TestD extends TestA{     
    //compiles error because Exception thrown by current method is not inherited from IOException which is thrown by the overrided method of the base class     
    void method() throws Exception{}     
}    
4. 构造器不遵循上述规则,因为构造器不遵循JAVA的覆盖和重载规则。
class TestA {  
    public TestA() throws IOException {}  
  
    public TestA(int i) {}  
}  
  
class TestC extends TestA {  
    // compiles fine if current constructor doesn't throw anything.  
    public TestC() { super(0); }  
}  
  
class TestB extends TestA {  
    // compiles fine even if current constructor throws exceptions which don't  
    // inherit from exceptions that are thrown by the overrided method of the  
    // base class  
    // this also means constructors don't conform the inheriting system of JAVA  
    // class  
    public TestB() throws Exception {}  
}  
5. 当一个类继承某个类,以及实现若干个接口,而被继承的类与被实现的接口拥有共同的方法,并且该方法被覆盖时,它所声明抛出的异常必须与它父类以及接口一致。
class ExceptionA extends Exception{  
}  
class ExceptionB extends Exception{  
      
}  
interface TestA{  
    void method() throws ExceptionA;  
}  
abstract class TestB{  
    abstract void method() throws ExceptionB;  
}  
class TestC extends TestB implements TestA{  
    //compiles error  
    public void method() throws ExceptionA{}  
}  
class TestD extends TestB implements TestA{  
    //compiles error  
    public void method() throws ExceptionB{}  
}  
class TestE extends TestB implements TestA{  
    //compiles error  
    public void method() throws ExceptionA,ExceptionB{}  
}  
class TestF extends TestB implements TestA{  
    //compiles fine  
    public void method(){}  
}  
原文地址:https://www.cnblogs.com/xiarongjin/p/8308389.html