user2223211 user2223211 - 28 days ago 9
Java Question

Java Exception Classes

I am new to Java and I was looking at exception handling. When we catch java exceptions, we declare and use an

Object
of the
Exception
class without initializing it, i.e.

catch(NullPointerException e)
e.printStackTrace();


So my question is, how are we able to use object reference e without instantiating it?

Answer Source

They are well instantiated:

void example() {
    throw new UnsupportedOperationException("message");
}      // ^^^

void demonstration() {
    try {
       example();
    } catch (UnsupportedOperationException e) {
        e.printStackTrace();
    }
}

This very simple example should be pretty self explanatory...