简体   繁体   中英

Handling Java custom exceptions

UPDATE:

I've fixed the syntax error and removed the exception handling, and I'm gettint the exact same error message (different line number, of course):

class Point {
    public Point (int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void displayPointPosition() {
        System.out.println("Position: " + x + " " + y);
    }
    private int x, y;
}

public class Exinfo2 {
    public static void main(String args[]) {
        Point a = new Point(1, 4);
        a.displayPointPosition();
        a = new Point(-3, 5);
        a.displayPointPosition();
    }
}

This really must be a beginner's mistake, and my code editor (Visual Studio Code) doesn't highlight anything wrong...

==============

I'm learning Java and I'm now trying to handle custom exceptions, but without success. I'm having various error messages when compiling my code, and I even have an issue from a sample that I copied from a book, so it seems like there might be something else than just the code...

This is the sample that I'm trying to make work, and I'm getting the compiler message:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

        at Exinfo2.main(Point.java:23)".

This is the code:

class Point {
    public Point (int x, int y) throws ErrConst {
        if ( (x < 0) || (y < 0) ) {
          throw new ErrConst ("Constructor error:" + x + " " + y);
        }
        this.x = x;
        this.y = y;
    }

    public void displayPointPosition() {
        System.out.println("Position: " + x + " " + y);
    }
    private int x, y;
}

class ErrConst extends Exception {
    ErrConst(String mes) {
        super(mes);
    }
}

public class Exinfo2 {
    public static void main(String args[]) {
        try {
            Point a = new Point(1, 4);
            a.displayPointPosition();
            a = new Point(-3, 5);
            a.displayPointPosition();
        }
        catch (ErrConst e) {
            System.out.println(e.getMessage());
            System.exit(-1);
        }
    }
}

I'm sure that this is very basic, but it's a headache for me, so I'm hoping that a kind coder could take a few minutes to help me out... :-)

Thanks in advance! :-)

Your code runs fine when i try to run it.

However, according to the exception you posted, it looks like you have saved the file as Point.java . You have several classes in the same file, but your public class is called Exinfo2 . In Java, the filename and the name of the public class must be the same, so that's probably the problem.

Try saving the file as Exinfo2.java .


The error message you get from the compiler is strange, though. Normally you would get the error message "class Exinfo2 is public, should be declared in a file named Exinfo2.java". It looks like the error you get is from the Eclipse compiler, so maybe the Visual Studio Code plugin is using that compiler instead of the regular one. Try compiling it like this from the command-line instead:

javac Exinfo2.java
java Exinfo2

I think that in your Point class..the method displayPointPosition .. the PrintStream not found your message.. can u try change it..for this : System.out.println("Position: " + x + " " + y);

regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM