Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

question on exception

  Asked By: Egidius    Date: May 25    Category: Java    Views: 553
  

in a try-catch() block if the exception thrown by the block does not
matches with the exception in the catch...will the code cease to
run??




what exactly NullPointerException means ?

suppose i am writing

String s = "hello";

s == null;

now if i write....

if(s == null && s.length() >0)
OR
if(s != null || s.length() >0)

probabily these will throw NullPointerException ...right?
is it just bcoz null string does not have length?? ...but i can say
null string has length 0. i.e s.length = 0 when s==null...whats
wrong?

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Anna Hill     Answered On: May 25

1.) The answer for the first question  is that it depends on whether or
not the exception  which is thrown is a checked or unchecked exception
and whether or not it inherits from the class which is being caught. If
it is an unchecked exception or it does inherit from the caught
exception's class then it will compile fine. If it does not then you
will not be able to compile your program.

2.) NullPointerException means  that the reference you are using to the
object does not exist. In your example the reason you will get an NPE
is that the String s does not exist. You suggest that you can execute
something which says s.length = 0 even though s is not defined, but I
find this hard to believe. This is even harder to believe since String
has no length  property.

 
Answer #2    Answered By: Alexander Fields     Answered On: May 25

your answer of the first question  is not clear to me.

say,i am trying to do..

class xyz{
....
try
{

//code block


}catch(ArithmeticException e)

{
System.out.println("arithmetic exception");
}

//blah

//blah

}


now say, code block  is throwing NullPointerException.....so it wont
be caught by the catch()....in this situation will the code  run??

you are talking about compilation errors....but i think it would be
runtime error...right?

 
Answer #3    Answered By: Vivian Ruiz     Answered On: May 25

NullPointerException is a RuntimeException, i.e. an unchecked
exception. Therefore your program will compile fine and if it throws a
NullPointerException during execution then your program may or may not
exit depending on what you are doing with threads. For example, if you
never launch any threads then your program will exit. However if you
have an AWT or Swing user interface and the runtime error occurs there
then your program will continue running.

 
Answer #4    Answered By: Harold Graham     Answered On: May 25

i wanted to know about finally block......whether the
exception is caught or not caught...is it true finally block  will run
always??

as in my example, i told that nullpointerexception  has occured but
try -catch could not catch  it....in this situation will the code
always run finally block?? or is it also depend upon the code?

 
Answer #5    Answered By: Giovanna Silva     Answered On: May 25

Finally blocks are always executed, whether the exception  is caught or
not, or even if an exception does not occur (i.e. even in the case of a
return).

 
Answer #6    Answered By: Aaliyah Khan     Answered On: May 25

All uncaught exceptions will be thrown to the upper encompassing layer
till it is resolved by some try-catch block  that is addressing this
exception; else you will end the program with the exception  trace on the
console/log file. Hope this helps.

 
Answer #7    Answered By: Maurice Hanson     Answered On: May 25

No, sorry, but that is not true. First of all only unchecked exceptions
will propagate without the proper method-level declaration. Second, if
you are writing a multi-threaded application, including any UI
application, then it is possible for threads to die with uncaught
exceptions while the application continues to run.

 
Answer #8    Answered By: Bellona Lopez     Answered On: May 25

finally will always be executed whatever be the case.

 
Didn't find what you were looking for? Find more on question on exception Or get search suggestion and latest updates.




Tagged: