Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 875
user2223211 Avatar asked Jun 11 '13 21:06

user2223211


1 Answers

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...

like image 186
jlordo Avatar answered Oct 18 '22 00:10

jlordo