*
The moose likes Beginning Java and the fly likes Nullpointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nullpointer Exception" Watch "Nullpointer Exception" New topic
Author

Nullpointer Exception

Aditi Gandy
Ranch Hand

Joined: Apr 23, 2008
Posts: 37
Hi every body.
I have attended a interview, in that, they asked me,

"Can we handle Nullpointer exception in finally block? Will it compile or not?"

I searched in the net, i am getting explanations for checked and unchecked exceptions......but, not yet got the proper answer for it.
Can anyone please help?


Thanks & Regards,
Author
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You can very quickly write a small test program to see if it works or not.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Aditi Gandy
Ranch Hand

Joined: Apr 23, 2008
Posts: 37
I am very new to that, can you pleae help me out with that small program?
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
You can catch it.



Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Manuel, the question was "Can we handle Nullpointer exception in finally block?"


[My Blog]
All roads lead to JavaRanch
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hi Ashvini,

First, NullPointerException is unchecked exception.So if there is a threat of throwing a NullPointer exception in your programme then also it will compile.

Second, whatever you write in finally block will be executed even in the case of exception.Generally, for example, If we are working with file manipulation then file closing and that kinda stuff we keep in finally block.It's not to handle the exception.Here, catch block is at your rescue.In catch block, we can handle the exeption and take necessary steps.

Does it Look very bookish funda?hmm..

Now, If i stick with your words then it's not possible to handle any exception in finally block because it's not his babby at all.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Yes we can (but it might not be the best approach)



Does it compile? yes it does
Can we deal with the exception in the finally block? yes we can (almost sounds like Barack Obama campaign slogan )
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Vishal Pandya: It's not to handle the exception.Here, catch block is at your rescue.


Sun's official documentation says :
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling � it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Vishal Pandya: Now, If i stick with your words then it's not possible to handle any exception in finally block because it's not his babby at all.


So, you indeed can do exception handling in finally but, it's not exactly the best place.



PS.
it's not his babby at all
I think there's no word as babby. Have a look at using real words.
[ April 23, 2008: Message edited by: Anubhav Anand ]
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Manuel Leiria: Can we deal with the exception in the finally block? yes we can (almost sounds like Barack Obama campaign slogan


Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Another brick in the wall :
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by Anubhav Anand:
Another brick in the wall :


A hole in that wall

Here, you have considered only one string variable. Let's assume that you have a bunch of string variables, a bunch of string arrays and many more. Now start checking null value condition for all of them.

Let's say any how you did it. But your programme throws ArithmeticException.
Now, i think you will require a bunch of boolean variables like blnException1,blnException2...

But be afraid from catch block.

[ April 23, 2008: Message edited by: Vishal Pandya ]
[ April 23, 2008: Message edited by: Vishal Pandya ]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by Manuel Leiria:
Yes we can (but it might not be the best approach)



Does it compile? yes it does
Can we deal with the exception in the finally block? yes we can (almost sounds like Barack Obama campaign slogan )



Somebody has just said that catch block is more appropriate than finally block to handle the exception according to sun's official document.And If you are using catch block why to handle exception in finally block.

Is anybody gonna handle the exception in this way?

Originally posted by ashvini pradeep:

I have attended a interview, in that, they asked me,

"Can we handle Nullpointer exception in finally block? Will it compile or not?"


I think the interviewer is more interested into knowing the importance of finally block and how actually the exceptions are handled.
[ April 23, 2008: Message edited by: Vishal Pandya ]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by Anubhav Anand:
I think there's no word as babby. Have a look at using real words.

[ April 23, 2008: Message edited by: Anubhav Anand ]


I will take care of this,baby.
Aditi Gandy
Ranch Hand

Joined: Apr 23, 2008
Posts: 37
Thank you very much Vishal Pandya, Manuel Leiria, Christophe Verre and Anubhav Anand.
Thank you for your Help, answer and your valueable time.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by Vishal Pandya:
A hole in that wall
...........................
But be afraid from catch block.


Originally posted by Vishal Pandya:
Somebody has just said that catch block is more appropriate than finally block to handle the exception according to sun's official document.And If you are using catch block why to handle exception in finally block.

Is anybody gonna handle the exception in this way?




I think you got to EASE UP dude. No offenses meant. There's nothing to get so wild about it. Everyone is free to express it his way and you shouldn't be so offended if anyone other than you feels some other way round.

The question was can we handle NullPointerException in finally block.
And the simple answre is, Yes we can. We indeed did give a though if that was the best way or not. Yes, catch block is the best way to do it but, if you wan't to handle it in finally block(keep aside the complexity) you can and you may do that.

Again no offenses meant.
 
Don't get me started about those stupid light bulbs.
 
subject: Nullpointer Exception
 
Similar Threads
[newbie] Null pointer exception ??
servlet doubt
@Autowire is not working properly in my spring application
JDBC/Servlets question
another mock exam question