0

I understand that checked exceptions inherit from Exception and that unchecked exceptions extend RuntimeException. But looking at the source code, I can't see what's in RuntimeException that makes it unchecked.

In fact, if I make my own class called MyRuntimeException and literally copy-paste the code from RuntimeException (after stripping out the package declaration and changing the class name), the resulting class will be checked. That is, when I try to throw a MyRuntimeException, the compiler will complain that the exception needs to be caught or declared to be thrown. However, throwing a RuntimeException compiles without error.

What magic pixie dust is in the built-in RuntimeException that makes it unchecked?

Edit: Is it possible for me, as an end-user programmer, to build my own Exception hierarchy and replicate the behavior of checked and unchecked exceptions?

Barry Brown
  • 20,233
  • 15
  • 69
  • 105

1 Answers1

3

The pixie dust is not in the RuntimeException class. It is in the compiler that treats RuntimeException and its derived classes differently from other exceptions.

Andrew Stein
  • 12,880
  • 5
  • 35
  • 43