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! :-)
System.out.println(Position: " + x + " " + y);doesn't look like valid Java - you're missing an opening quote beforePosition