-3

Ok so I am trying to build a exception handler, and I can't for the life of me, figure out why its not working! I did more or less the exact same thing on a previous assignment and it worked fine.

So this is the exception handler class

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");
    }




}

and then to test a string I used a method that creates a pattern

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;
}

It is making sure a phone number is entered correctly. I've tested the method and it works fine.

BUT, when I do and if statement such as

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

I get a unhandled exception error and it just crashes pointing to the line that calls the BadAppointmentDataException as the failure!

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 1
    show the error message with stack trace – Ryuzaki L Nov 26 '19 at 20:58
  • 1
    Are you talking about a runtime error? You haven't included the code with the try-catch block where you handle the exception you're throwing. – azurefrog Nov 26 '19 at 21:00
  • 1
    Please show us a [Minimal Complete and Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) instead of snippets of code. – omajid Nov 26 '19 at 21:03
  • It doesn't crash, because it doesn't run, and it doesn't run because it doesn't compile. This is a compilation error. And `RuntimeExceptions` have nothing to do with it. – user207421 Nov 26 '19 at 21:04
  • Are you sure you're using the correct exception class, then? Using the class you posted, `void test() { throw new BadAppointmentDataException("foo", "bar"); }` compiles just fine for me. Maybe you've got your previous assignment in your classpath, with a `class BadAppointmentDataException extends Exception` in there? – azurefrog Nov 26 '19 at 21:05
  • Also, whether it's a compile- or run-time error, it's always a good idea to include the full text of the error message in your question. – azurefrog Nov 26 '19 at 21:07
  • @user207421 "It doesn't crash, because it doesn't run, and it doesn't run because it doesn't compile." - I was able to compile that code. – Jeff Scott Brown Nov 26 '19 at 22:02
  • thanks guys, I'm sure you can tell I am a n00b but thanks to Jeff Scott Brown below I was able to get err fixed! I'm really astonished at how fast you guys get to work on this though! Appreciated! – explicitcorp Nov 27 '19 at 08:02
  • "...thanks to Jeff Scott Brown below I was able to get err fixed" - Awesome. Glad you got it straightened out. Welcome to StackOverflow. – Jeff Scott Brown Nov 27 '19 at 14:36

1 Answers1

1

Your BadAppointmentDataException class is not an exception handler. It is an exception. You have to write code to handle the exception, typically in a catch block. For example, this will result in an unhandled exception:

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;
    }
}

This will handle the exception:

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;
    }
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • ok thank you! I was wondering about that, like I wasn't sure if I needed the try catch block or not, I tried it a few times as well but I'm thinking I did it in the wrong area. – explicitcorp Nov 27 '19 at 03:56