Java 异常处理程序设计问题

Java 异常处理程序设计问题,java,exception,runtimeexception,Java,Exception,Runtimeexception,好的,所以我正在尝试构建一个异常处理程序,但我一辈子都无法弄清楚它为什么不起作用!我在上一次任务中做了差不多相同的事情,效果很好 这就是异常处理程序类 package cst8284.asgmt3.scheduler; public class BadAppointmentDataException extends RuntimeException{ private static final long serialVersionUID = 1L; private Strin

好的,所以我正在尝试构建一个异常处理程序,但我一辈子都无法弄清楚它为什么不起作用!我在上一次任务中做了差不多相同的事情,效果很好

这就是异常处理程序类

package cst8284.asgmt3.scheduler;


public class BadAppointmentDataException extends RuntimeException{
    private static final long serialVersionUID =  1L;
    private String Description;

    public String getDescription() {
        return Description;
    }

    public void setDescription(String Description) {
        this.Description = Description;
    }
    public BadAppointmentDataException(String m, String e) {
        super(m);
        this.setDescription(e);
    }
    public BadAppointmentDataException() {
        this("Please Try Again","Bad Data Entered");
    }




}
然后为了测试一个字符串,我使用了一个创建模式的方法

private static boolean testPhone(String p) {
    Pattern pnum = Pattern.compile("\\d{3}\\-\\d{3}\\-\\d{4}"); 
    Matcher m = pnum.matcher(p);
    boolean b = m.matches();   
    return b;
}
它确保正确输入电话号码。我已经测试了这个方法,效果很好

但是,当我这样做时,如果

if (!testPhone(phoneNumber)){
    throw new BadAppointmentDataException("why doesn't this","work");
}

我得到一个未处理的异常错误,它只是指向调用
BadAppointmentDataException
作为失败的行崩溃

您的
BadAppointmentDataException
类不是异常处理程序。这是个例外。您必须编写代码来处理异常,通常是在
catch
块中。例如,这将导致未处理的异常:

package demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {

    public static void main(String[] args) {
        // this will result in an unhandled exception
        doSomething();
    }

    private static void doSomething() {
        if (!testPhone("some bad phone number")) {
            throw new BadAppointmentDataException("why doesn't this", "work");
        }
    }

    private static boolean testPhone(String p) {
        Pattern pnum = Pattern.compile("\\d{3}\\-\\d{3}\\-\\d{4}");
        Matcher m = pnum.matcher(p);
        boolean b = m.matches();
        return b;
    }
}
package demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {

    public static void main(String[] args) {
        try {
            doSomething();
        } catch (BadAppointmentDataException exc) {
            // put your exception handling logic here...
            System.err.println("An error has occurred");
        }
    }

    private static void doSomething() {
        if (!testPhone("some bad phone number")) {
            throw new BadAppointmentDataException("why doesn't this", "work");
        }
    }

    private static boolean testPhone(String p) {
        Pattern pnum = Pattern.compile("\\d{3}\\-\\d{3}\\-\\d{4}");
        Matcher m = pnum.matcher(p);
        boolean b = m.matches();
        return b;
    }
}
这将处理异常:

package demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {

    public static void main(String[] args) {
        // this will result in an unhandled exception
        doSomething();
    }

    private static void doSomething() {
        if (!testPhone("some bad phone number")) {
            throw new BadAppointmentDataException("why doesn't this", "work");
        }
    }

    private static boolean testPhone(String p) {
        Pattern pnum = Pattern.compile("\\d{3}\\-\\d{3}\\-\\d{4}");
        Matcher m = pnum.matcher(p);
        boolean b = m.matches();
        return b;
    }
}
package demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {

    public static void main(String[] args) {
        try {
            doSomething();
        } catch (BadAppointmentDataException exc) {
            // put your exception handling logic here...
            System.err.println("An error has occurred");
        }
    }

    private static void doSomething() {
        if (!testPhone("some bad phone number")) {
            throw new BadAppointmentDataException("why doesn't this", "work");
        }
    }

    private static boolean testPhone(String p) {
        Pattern pnum = Pattern.compile("\\d{3}\\-\\d{3}\\-\\d{4}");
        Matcher m = pnum.matcher(p);
        boolean b = m.matches();
        return b;
    }
}

显示带有堆栈跟踪的错误消息您在说运行时错误吗?您没有将代码包含在处理抛出的异常的try-catch块中。请向我们显示一个而不是代码片段。它不会崩溃,因为它不运行,也不会运行,因为它不编译。这是一个编译错误。和
RuntimeExceptions
与此无关。那么,您确定使用了正确的异常类吗?使用您发布的类,
void test(){throw new BadAppointmentDataException(“foo”,“bar”);}
对我来说编译得很好。也许您的类路径中有您以前的作业,其中有一个
class BadAppointmentDataException extensed Exception
?好的,谢谢!我很好奇,好像我不确定我是否需要试抓挡块,我也试了几次,但我想我做得不对。