处理Java自定义异常

huangapple go评论61阅读模式
英文:

Handling Java custom exceptions

问题

更新

我已经修复了语法错误并移除了异常处理但是我得到了完全相同的错误信息当然是不同的行号):

    class Point {
        public Point (int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        public void displayPointPosition() {
            System.out.println("位置:" + 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();
        }
    }

这肯定是初学者的错误我的代码编辑器Visual Studio Code没有标出任何错误...

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

我正在学习Java现在我正在尝试处理自定义异常但是没有成功在编译我的代码时我遇到了各种错误消息甚至在我从一本书上复制的示例中也出现了问题所以看起来问题可能不仅仅是代码...

这是我试图使其正常工作的示例但是我得到了编译器的消息

    "Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
            
            at Exinfo2.main(Point.java:23)".

这是代码

    class Point {
        public Point (int x, int y) throws ErrConst {
            if ( (x < 0) || (y < 0) ) {
              throw new ErrConst ("构造函数错误:" + x + " " + y);
            }
            this.x = x;
            this.y = y;
        }
    
        public void displayPointPosition() {
            System.out.println("位置:" + 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);
            }
        }
    }

我确信这非常基础但是对我来说很头疼所以我希望一位友善的程序员能花几分钟帮助我... :-)

提前感谢 :-)
英文:

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(&quot;Position: &quot; + x + &quot; &quot; + 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:

&quot;Exception in thread &quot;main&quot; java.lang.Error: Unresolved compilation problem: 
at Exinfo2.main(Point.java:23)&quot;.

This is the code:

class Point {
public Point (int x, int y) throws ErrConst {
if ( (x &lt; 0) || (y &lt; 0) ) {
throw new ErrConst (&quot;Constructor error:&quot; + x + &quot; &quot; + y);
}
this.x = x;
this.y = y;
}
public void displayPointPosition() {
System.out.println(&quot;Position: &quot; + x + &quot; &quot; + 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... 处理Java自定义异常

Thanks in advance! 处理Java自定义异常

答案1

得分: 2

你的代码在我尝试运行时运行良好。

然而,根据你发布的异常信息,看起来你将文件保存为 Point.java。你在同一个文件中有几个类,但你的公共类名为 Exinfo2。在Java中,文件名和公共类名必须相同,所以这可能是问题所在。

尝试将文件保存为 Exinfo2.java


然而,你从编译器那里得到的错误消息很奇怪。通常你会得到错误消息:“class Exinfo2 is public, should be declared in a file named Exinfo2.java”。看起来你得到的错误是来自Eclipse编译器,所以也许Visual Studio Code插件正在使用那个编译器而不是常规的。尝试使用命令行编译它:

javac Exinfo2.java
java Exinfo2
英文:

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

答案2

得分: 1

我认为在你的Point类中...方法displayPointPosition...PrintStream未找到你的消息..
你可以尝试将它更改为这个:System.out.println("Position: " + x + " " + y);

祝好!

英文:

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!

huangapple
  • 本文由 发表于 2020年9月18日 11:29:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63948939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定