有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中定义自定义异常类,这是最简单的方法?

我试图以最简单的方式定义自己的异常类,这就是我得到的:

public class MyException extends Exception {}

public class Foo {
  public bar() throws MyException {
    throw new MyException("try again please");
  }
}

Java编译器是这样说的:

cannot find symbol: constructor MyException(java.lang.String)

我觉得这个构造函数必须从^{}继承,不是吗


共 (6) 个答案

  1. # 1 楼答案

    异常类有两个构造函数

    • public Exception()——这构造了一个异常,没有任何附加信息。异常的性质通常是从类名推断出来的
    • public Exception(String s)--构造一个带有指定错误消息的异常。详细信息是描述此特定异常的错误条件的字符串
  2. # 2 楼答案

    如果在Eclipse中使用new class对话框,只需将Superclass字段设置为java.lang.Exception,并选中“Constructors from Superclass”,它将生成以下内容:

    package com.example.exception;
    
    public class MyException extends Exception {
    
        public MyException() {
            // TODO Auto-generated constructor stub
        }
    
        public MyException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
        public MyException(Throwable cause) {
            super(cause);
            // TODO Auto-generated constructor stub
        }
    
        public MyException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
    }
    

    针对以下关于不在defualt构造函数中调用super()的问题,Oracle有this to say

    Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

  3. # 3 楼答案

    我定义的一个典型的自定义异常如下:

    public class CustomException extends Exception {
    
        public CustomException(String message) {
            super(message);
        }
    
        public CustomException(String message, Throwable throwable) {
            super(message, throwable);
        }
    
    }
    

    我甚至用Eclipse创建了一个模板,这样我就不用一遍又一遍地写所有的东西了

  4. # 4 楼答案

    package customExceptions;
    
    public class MyException extends Exception{
    
        public MyException(String exc)
        {
            super(exc);
        }
        public String getMessage()
        {
            return super.getMessage();
        }
    }
    

    import customExceptions.MyException;
    
    public class UseCustomException {
    
        MyException newExc=new MyException("This is a custom exception");
    
        public UseCustomException() throws MyException
        {
            System.out.println("Hello Back Again with custom exception");
            throw newExc;       
    }
    
        public static void main(String args[])
        {
            try
            {
                UseCustomException use=new UseCustomException();
            }
            catch(MyException myEx)
            {
                System.out.println("This is my custom exception:" + myEx.getMessage());
            }
        }
    }
    
  5. # 5 楼答案

    不,您不需要“继承”非默认构造函数,您需要在类中定义一个接受字符串的构造函数。通常,在构造函数中使用super(message)来调用父构造函数。例如,像这样:

    public class MyException extends Exception {
        public MyException(String message) {
            super(message);
        }
    }
    
  6. # 6 楼答案

    Java平台的Inheritance文章解释了这一点的原因,文章说:

    "A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass."