Can Constructor Throw Exceptions in Java?

1. The question In Java, methods can throw exceptions. Can constructors also throw exceptions? The answer is YES. 2. The reason and some necessary background A constructor is just a special method. In this perspective, it surely can do what regular methods can do. It is possible that there are some objects created and assigned … Read more

2 Examples to Show How Java Exception Handling Works

There are 2 examples below. One shows all caller methods also need to handle exceptions thrown by the callee method. The other one shows the super class can be used to catch or handle subclass exceptions. Caller method must handle exceptions thrown by the callee method Here is a program which handles exceptions. Just test … Read more

Diagram of Exception Hierarchy

In Java, exception can be checked or unchecked. They both fit into a class hierarchy. The following diagram shows Java Exception classes hierarchy. Red colored are checked exceptions. Any checked exceptions that may be thrown in a method must either be caught or declared in the method’s throws clause. Checked exceptions must be caught at … Read more