This question already has answers here:
How can I write custom Exceptions?
                                
                                    (4个答案)
                                
                        
                                4年前关闭。
            
                    
可以将以下代码视为自定义异常的示例吗?
如果没有,请您从您的角度解释原因?

public class UseJDBC
{
    public useJDBC() throws Exception
    {
        throw new Exception("ABC");
    }

    public static void main(String args[])
    {
        try
        {
            useJDBC a = new useJDBC();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

最佳答案

不它不是。如果要创建自定义异常,则必须扩展java.lang.Exception或其子类之一。见https://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html

10-06 01:05