Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java自定义异常_Java_Exception_Exception Handling - Fatal编程技术网

Java自定义异常

Java自定义异常,java,exception,exception-handling,Java,Exception,Exception Handling,我已使用下面的代码成功地实现了一个自定义异常 CarNotFoundException.Java public class CarNotFoundException extends Exception { private static final long serialVersionUID = 1L; public CarNotFoundException(String msg) {

我已使用下面的代码成功地实现了一个自定义异常

CarNotFoundException.Java

 public class CarNotFoundException extends Exception 
       {        
            private static final long serialVersionUID = 1L;

            public CarNotFoundException(String msg) {
                super(msg);
            }
        }
Car.Java

public static CarProvider getInstanceByProvider(String provider) throws CarNotFoundException {
        if(!provider.equals(Constants.BMW || Constants.B||Constants.C{ 
            throw new CarNotFoundException("Car Not Found");
            }   
        return carProvider;
    }
        try 
        {
          carProvider = Car.getInstanceByProvider(provider);    
        } catch (CarNotFoundException e) {
            e.printstacktrace();
        }
CarTest.java

public static CarProvider getInstanceByProvider(String provider) throws CarNotFoundException {
        if(!provider.equals(Constants.BMW || Constants.B||Constants.C{ 
            throw new CarNotFoundException("Car Not Found");
            }   
        return carProvider;
    }
        try 
        {
          carProvider = Car.getInstanceByProvider(provider);    
        } catch (CarNotFoundException e) {
            e.printstacktrace();
        }
我想做什么?

而不是
e.printStackTrace()当我调用
e.getMessage()
时, 我什么也得不到(空白)

如何定制
e.getMessage()

编辑:我得到了答案,但错过了
System.out.println()


谢谢你的帮助

您的异常类中缺少了
getMessage
方法。您需要一个get方法来打印并传递它

编辑
像这样:

  public String getMessage() {
            String var1 = super.getMessage();
       }
super.getMessage()
是Throwable类中的
getMessage()
重写自定义异常中的getMessage()方法

public class CarNotFoundException extends Exception 
{        
     private static final long serialVersionUID = 1L;

     public String message;

     public CarNotFoundException(String msg) {
         this.message = msg;
     }

  // Overrides Exception's getMessage()
     @Override
     public String getMessage(){
         return message;
     }
 }

此ProviderNotFoundException是什么?是自定义的吗?您的自定义异常似乎是
CarNotFoundException
,但您正在捕获一个
ProviderNotFoundException
e.getMessage()
将与
CarNotFoundException
中的代码配合使用。希望您早点说出来。我已经发布了一个答案,您确定您正在调用e.getMessage()吗?上面显示的代码没有问题。
e.getMessage()
将获取消息。你在打印吗<代码>System.out.println(e.getMessage())这是你的答案添加了
公共字符串getMessage(){return“#pcp message”}
不起作用。这是一条相当于“嗨,你好”的简单消息感谢你帮助我得到答案,我错过了打印它这完全没有必要,因为类似的代码已经存在于
Throwable
中;您的答案中的代码也未编译。已尝试此操作,但不起作用
公共类CarNotFoundException扩展异常{private static final long SerialVersionId=1L;私有字符串消息;公共CarNotFoundException(字符串消息){this.message=message;}@Override public String getMessage(){return message;}}
你收到了什么消息?我什么也没有得到,空白的messageWrite System.out.print(e.getMessage());这完全没有必要,因为类似的代码已经存在于
Throwable
中。