About Question enthuware.ocajp.i.v7.2.1048 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1048 :

Post by ETS User »

Why throw NullPointerException whe RUn?

Tank you

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by admin »

NullPointerException is thrown by the statement throw re; because re is null. This will be caught by the catch block, which prints the exception.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

adrian110288
Posts: 12
Joined: Thu Aug 01, 2013 3:44 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by adrian110288 »

admin wrote:NullPointerException is thrown by the statement throw re; because re is null. This will be caught by the catch block, which prints the exception.

HTH,
Paul.

Still dnt quite understand it :/ Is NullPointerException caught, because the try block throws null value? I thought the NullPointerException is thrown when we try to access an objec which is null, but here we dnt try to access the value thrown by try block, do we? im confused ...

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by admin »

Yes, the NPE is thrown because of the statement throw re, where re is null.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ad9999
Posts: 15
Joined: Fri May 15, 2015 12:06 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by Ad9999 »

throw null; = throw new NullPointerException();

Cool. Null is so versatile.

gianmarcoelg
Posts: 1
Joined: Wed Sep 06, 2017 12:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by gianmarcoelg »

You could found inside the NullPointerException class the following:
*Throwing {@code null} as if it were a {@code Throwable} value.

Regards.
gmeg

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by __JJ__ »

Hi
A NullPointerException will be thrown if the expression given to the throw statement results in a null pointer.
This is very interesting. I had thought the throw would throw a null object which would nonetheless be caught by the catch but that the attempt to print it in the SOP (calling e.toString() maybe) would cause an NPE.
I had to verify it for myself. To drive the point home:


Code: Select all

        try{
            IOException e = null;
            throw e;
        }
        catch(IOException e){
            System.out.println("UCK IOE");
            System.out.println(e);
        }
        catch(NullPointerException e){
            System.out.println("UCK NPE");
            System.out.println(e);
        }
        catch(IllegalArgumentException e){
            System.out.println("UCK IAE");
            System.out.println(e);
        }
        catch(RuntimeException e){
            System.out.println("UCK RTE");
            System.out.println(e);
        }

prints

Code: Select all

UCK NPE
java.lang.NullPointerException

MariaRoxana
Posts: 2
Joined: Sun Dec 09, 2018 10:33 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by MariaRoxana »

Hy,

Can someone, please, explain to me, why System.out.println(e) outputs java.lang.RuntimeException? I remember that println(Object o) will do String.valueOf(x) to get the printed object's string value but x is null, so it prints null because of print(String x). I tried to read the documentation but I didn't understand very well the idea.

Thx :)

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1048 :

Post by admin »

You remember correctly that if the argument to SOP is null, then null is printed. However, in the given code, e is not null. The statement "throw re;" in the try block does not send a null reference to the catch block. Instead, this statement actually causes a NullPointerException. That is why, the e in catch(Exception e) does not get a null but a reference to a valid NullPointerException object, which is what is printed by the SOP statement.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 64 guests