About Question enthuware.ocajp.i.v7.2.1048 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1048 :
Why throw NullPointerException whe RUn?
Tank you
Tank you
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Thu Aug 01, 2013 3:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
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 ...
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Yes, the NPE is thrown because of the statement throw re, where re is null.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Fri May 15, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
throw null; = throw new NullPointerException();
Cool. Null is so versatile.
Cool. Null is so versatile.
-
- Posts: 1
- Joined: Wed Sep 06, 2017 12:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
You could found inside the NullPointerException class the following:
*Throwing {@code null} as if it were a {@code Throwable} value.
Regards.
gmeg
*Throwing {@code null} as if it were a {@code Throwable} value.
Regards.
gmeg
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
Hi
I had to verify it for myself. To drive the point home:
prints
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.A NullPointerException will be thrown if the expression given to the throw statement results in a null pointer.
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
-
- Posts: 2
- Joined: Sun Dec 09, 2018 10:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1048 :
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.
Who is online
Users browsing this forum: No registered users and 64 guests