即使我们不处理 Frame 构造函数抛出的已检查异常,程序也会遵守吗?
Program complies even when we do not handle the checked Exception thrown by Frame constructor?
根据 Oracle 文档 https://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html#Frame()
Frame 构造函数抛出 HeadlessException - 当 GraphicsEnvironment.isHeadless() returns true
但是调用构造函数时程序运行时没有处理异常
import java.awt.*;
import java.awt.event.*;
public class MouseDemo extends Frame{
String msg="";
MouseDemo()
{
super();//not handling HeadlessException
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
msg="Clicked";
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
} );
}
@Override
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawString(msg, 50, 50);
}
public static void main(String agrs[])
{
MouseDemo obj=new MouseDemo();
obj.setSize(new Dimension(300,300));
obj.setTitle("hello");
obj.setVisible(true);
}
}
MouseDemo() 既不处理 HeadlessException 也不将其抛给调用方法那么为什么我们没有得到编译错误
正如 Java 语言规范的 §8.4.6 所说,
It is permitted but not required to mention unchecked exception classes (§11.1.1) in a throws
clause.
因此,并非您在 throws
子句中看到的所有内容都是已检查的异常。事实上,已检查的异常定义在 §11.1.1:
RuntimeException
and all its subclasses are, collectively, the run-time exception classes.
The unchecked exception classes are the run-time exception classes and the error classes.
The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are Throwable
and all its subclasses other than RuntimeException
and its subclasses and Error
and its subclasses.
如果你查看HeadlessException
的继承树,你会发现它是RuntimeException
的子类:
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.UnsupportedOperationException
java.awt.HeadlessException
因此,这是一个未经检查的异常,您不需要使用 try...catch
或另一个 throws
子句来处理它。
根据 Oracle 文档 https://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html#Frame()
Frame 构造函数抛出 HeadlessException - 当 GraphicsEnvironment.isHeadless() returns true
但是调用构造函数时程序运行时没有处理异常
import java.awt.*;
import java.awt.event.*;
public class MouseDemo extends Frame{
String msg="";
MouseDemo()
{
super();//not handling HeadlessException
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
msg="Clicked";
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
} );
}
@Override
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawString(msg, 50, 50);
}
public static void main(String agrs[])
{
MouseDemo obj=new MouseDemo();
obj.setSize(new Dimension(300,300));
obj.setTitle("hello");
obj.setVisible(true);
}
}
MouseDemo() 既不处理 HeadlessException 也不将其抛给调用方法那么为什么我们没有得到编译错误
正如 Java 语言规范的 §8.4.6 所说,
It is permitted but not required to mention unchecked exception classes (§11.1.1) in a
throws
clause.
因此,并非您在 throws
子句中看到的所有内容都是已检查的异常。事实上,已检查的异常定义在 §11.1.1:
RuntimeException
and all its subclasses are, collectively, the run-time exception classes.The unchecked exception classes are the run-time exception classes and the error classes.
The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are
Throwable
and all its subclasses other thanRuntimeException
and its subclasses andError
and its subclasses.
如果你查看HeadlessException
的继承树,你会发现它是RuntimeException
的子类:
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.UnsupportedOperationException
java.awt.HeadlessException
因此,这是一个未经检查的异常,您不需要使用 try...catch
或另一个 throws
子句来处理它。