• 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

Question on Exception

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

let's take the fallowing code

class exception{
public static void main(String[] a){
try{
int a=10;
int b=0;
int c=a/b;
}
catch(Exception e){
e.getMessage();
}
}
}

In the Aboue code i am not creating any Exception Object useing new Operator.. so, there is no Object.....

is it nothing but say Exception e=null;??
why e,getMessage() giving me nullpointer Exception??

can anybody explain this??


thanks in Advance.

regards
krishna
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you are getting a null pointer exception as I am getting the airthmetic exception as it is suppose to throw.
 
Krishna Bulusu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my question is why it is not giving "NullPointerException"???
here we r not creating Any Object of Exception.we simply declare it not initialize it.....
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the line int c=a/b, an object of ArithmeticException is automactically created at runtime ,whose reference is assigned to e,so you are not getting nullpointer exception while using e.getMessage().

i hope it is clear to you.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,

In Exceptions U need not throw Exceptions Explictly. when JVM encounters abnormal condition It creates Corresponding Exception Object and throw it.
programmer need not bother about it.

I hope you understood now.

Thanks&Regards
Venkat
 
reply
    Bookmark Topic Watch Topic
  • New Topic