• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nullpointer Exception

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can very quickly write a small test program to see if it works or not.
 
Aditi Gandy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very new to that, can you pleae help me out with that small program?
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can catch it.

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manuel, the question was "Can we handle Nullpointer exception in finally block?"
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another brick in the wall :
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic