对于一个学校项目,我创建了一个程序,它从文件中读取输入并构造一个条形图来表示输入中找到的一些数据。逐行读取输入,并为每一行更新条形图。对于已读取的每一行,(“line%d \ n”,lineNumber)将打印到控制台。所有这些功能都已实施,并且有效。但是,该程序会向控制台抛出异常,我希望它们消失。以下是控制台的摘录:
line 1
line 2
line 3
Exception in thread "AWT-EventQueue-0" line 4
line 5
java.lang.IllegalArgumentException: Color parameter outside of expected range: Red
at java.awt.Color.testColorValueRange(Color.java:310)
at java.awt.Color.<init>(Color.java:395)
at java.awt.Color.<init>(Color.java:369)
at ui.staafdiagram.StaafDiagramPanel.berekenKleur(StaafDiagramPanel.java:62)
at ui.staafdiagram.StaafDiagramPanel.tekenStaaf(StaafDiagramPanel.java:106)
at ui.staafdiagram.StaafDiagramPanel.paintComponent(StaafDiagramPanel.java:162)
at javax.swing.JComponent.paint(JComponent.java:1045)
at javax.swing.JComponent.paintChildren(JComponent.java:878)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:878)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
at javax.swing.JComponent.paint(JComponent.java:1031)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1967)
line at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)6
//more errors.... PS Note the '6' at the end of this block of code. Error message thrown from within out.printf("line %d\n", lineNumber)?
由于某种原因,我无法理解,每次使用相同的输入运行程序时,错误消息的抛出方式都不同,代码相同。例如,当我再次跑步时,我得到了这个:
line 1
line 2
line 3
line 4
line 5
line Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red
6
line at java.awt.Color.testColorValueRange(Color.java:310)
//more errors...
以下是我的代码的摘录:
private void processInput() {
Scanner input;
Scanner lineScanner = null; // initialization
String line;
input = new Scanner(System.in);
int lineNumber = 0;
while (input.hasNextLine()) {
lineNumber++;
out.printf("line %d\n", lineNumber);
line = input.nextLine();
lineScanner = new Scanner(line);
if (line.length() < MIN_OK_LINE_LENGTH)
continue;
incrementBar(gameResult(lineScanner));
barChart.showChanges();
}
input.close();
lineScanner.close();
}
如果请求,将发布更多控制台错误和程序代码。我的程序使用大学java库中的方法,任何不是英文的错误信息都源于我不熟悉代码的方法。
答案 0 :(得分:3)
错误消息指示调用了java.awt.Color构造函数,其中Red组件的值超出范围。查看颜色的javadoc,我们看到红色是8位值,即0-255之间。在代码中查找代表颜色的内容,并发现为什么它有时会产生一个(至少)红色超出此范围的颜色。如果某些东西正在读取数据,或者随机生成颜色值,那么这就解释了为什么它不会在一致的时间发生。
请注意,该值可能是int或float - 再次,查看Color的不同构造函数将告诉您预期的内容。我们不知道绿色和蓝色的颜色是有效的,只是红色不是。
答案 1 :(得分:1)
首先,我同意其他答案:您应该找到错误,修复它并将错误修复程序发送到库维护者/作者。您甚至可能从教授那里得到额外的功劳,您永远不会知道。在这种情况下,在ui.staafdiagram.StaafDiagramPanel
中的某处分配了错误的颜色值(不在0-255范围内)。
如果您忽略抛出未经检查的异常,您永远不会知道程序的哪些部分可能会中断,真的任何都可能发生!
但是,也许你有一个演示版,并且无法及时找到错误。例外通常打印到stderr,因此您可以完全忽略stderr的输出:
System.setErr(new PrintStream(new OutputStream() {
public void write(int b) throws IOException {
//nothing
}
}));
throw new RuntimeException("test"); //this should not be printed, ever.
请记住,这会吞没所有异常,您的程序可能会在没有警告且没有打印原因的情况下失败。