• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

PL HELP -EXAM TOMORROW

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class checkExcept
{
public static void main(String args[]) throws Exception {
Exception e = null;
System.out.println(e);
throw e;.
}
}
/*
G:\paddy\java\Jqplus590quest\jqplus\jj\Referencequest>javac checkExcept.java
G:\paddy\java\Jqplus590quest\jqplus\jj\Referencequest>java checkExcept
Exception in thread "main" java.lang.NullPointerException:
at checkExcept.main(checkExcept.java:6)
Problem: I presume if i put a throws in the main method, it acts as the defualt handler and if there is a checked exception it will handle it and hence no compile error. The same thing happens here to
Throw e is a checked exception which is handled.
Now why is NullPointerException thrown during runtime.
I understand that Exception e = null. But null can be assigned to any object. Also throw e, here e is a checked exception. So how come the nullpointerexception is thrown?
Pl help.
Thanks padmini
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am not good in Exception handLing, but here is my answer. Correct me if I am wrong.
NullPointerException is thrown because e is null. Here e is not ONLY checked exception. It can be any one its subclasses. Take a look at the code, it will throw InterruptedException.


Exception
|
|---------------------------|
Interrupted Run IO
|
|---------------|
Arith Null


When you throw an exception, and if there is no handler then it is the exception is handled by default handler.The default handler prints the name of the exception and the stack trace.


public class checkExcept
{
public static void main(String args[]) throws Exception {
InterruptedException e = new InterruptedException();
//null;
System.out.println(e);
throw e;
}
}


Vanitha.
[This message has been edited by Vanitha Sugumaran (edited July 08, 2001).]
[This message has been edited by Vanitha Sugumaran (edited July 08, 2001).]
When I type the message I am having a good format of the exception class & subclasses and its subclasses, but when I post it the format is not the same.
[This message has been edited by Vanitha Sugumaran (edited July 08, 2001).]
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all checked exceptions should be handled,either with throws clause in method signature or with try-catch block.otherwise compile time error.
if you are adding throws clause in method signature,the error has to be caught when method is called,other wise it will propagate and end up with runtime error.
in this case you have to use try-catch to make the program run fine.since exception is set to null,null pointer exception will be thrown instead of Exception.
 
Sheriff
Posts: 16145
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the expression for throw evaluates to null, a NullPointerException is created and thrown instead. In other words, you shouldn't try to throw a null exception.
See the JLS 14.17
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic