From: Jack on 25 Feb 2010 03:09 Does java NullPointerException always cause crash? Thanks.
From: Roedy Green on 25 Feb 2010 03:26 On Thu, 25 Feb 2010 00:09:22 -0800 (PST), Jack <junw2000(a)gmail.com> wrote, quoted or indirectly quoted someone who said : >Does java NullPointerException always cause crash? it never causes a crash. Only a big in the JVM causes a crash. If you don't catch is the run time will, and print out a stack trace. If you wanted to do something else you would do: try { .... } catch ( NullPointerExeception e ) { err.println( "Not again!"); e.printStackTrace( err ); } -- Roedy Green Canadian Mind Products http://mindprod.com The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair. ~ Douglas Adams (born: 1952-03-11 died: 2001-05-11 at age: 49)
From: Thomas Pornin on 25 Feb 2010 08:26 According to Roedy Green <see_website(a)mindprod.com.invalid>: > On Thu, 25 Feb 2010 00:09:22 -0800 (PST), Jack <junw2000(a)gmail.com> > wrote, quoted or indirectly quoted someone who said : > > >Does java NullPointerException always cause crash? > > it never causes a crash. Theoretically you are right. However I did encounter JVM implementations which occasionally had trouble with NullPointerException (including a port of Sun's JVM for FreeBSD). To be precise, in many JVM, null references are not checked before access; instead, the access is trapped by the OS, which generates a system-level exception (on Unix-like systems, a SIGSEGV or SIGBUS signal). The JVM intercepts that signal, obtains the faulty code address, and converts the signal into a NullPointerException which is then thrown in the offending thread. Interception of the signal, obtention of the faulty code address, and reconstruction of the offending opcode address and stack trace are very system-dependent. This depends on an awful lot of ill-defined details (such as how the OS decides where allocated blocks will be in the address space) which may change between OS versions. The main result is that conversion of null pointer accesses into NullPointerException is somewhat less robust than the rest of the JVM, especially in non-mainstream platforms. It is not the NullPointerException which crashes the JVM, but the JVM failure to throw a NullPointerException which implies the crash. Of course, a JVM which fails to convert a null pointer access into a NullPointerException is not a compliant implementation of the Java Virtual Machine. Nevertheless, such implementations exist, so, in the interest of portability, it is best to endeavour not to access null pointer references needlessly. A rather common idiom (in Sun's own source code, no less) is to test arguments explicitly. For instance, taken from java.io.Writer source: protected Writer(Object lock) { if (lock == null) { throw new NullPointerException(); } this.lock = lock; } Here, the purpose of the explicit test is to have an early NPE (so that it is thrown when this constructor is used, and not later on, when "lock" is used) but it also avoids the kind of bug I was explaining above. At my job, we tend to handle null pointer accesses as bugs, just like out-of-bounds array accesses: they are not meant to happen, never. In our public API, we add such explicit tests, so that offending callers are duly detected as soon as possible. --Thomas Pornin
From: Eric Sosman on 25 Feb 2010 08:47 On 2/25/2010 3:09 AM, Jack wrote: > Does java NullPointerException always cause crash? No. Yes. Mu. Define "crash." -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Lew on 25 Feb 2010 10:50 Jack wrote, quoted or indirectly quoted someone who said : >>> Does java [sic] NullPointerException always cause crash? According to Roedy Green : >> it never causes a crash. Thomas Pornin wrote: > Theoretically you are right. However I did encounter JVM implementations > which occasionally had trouble with NullPointerException (including a > port of Sun's JVM for FreeBSD). Come on, guys, Jcheeze! The OP clearly meant crash of his program, not the JVM. The answer is that uncaught exceptions will cause termination of a program. Ones you catch and deal with properly will not. 'NullPointerException', or "NPE" as some abbreviate it, is a runtime exception, that is, it is a subtype of 'RuntimeException'. Runtime exceptions generally represent a programmer mistake. A properly-written program will not throw an NPE. Runtime exceptions are designed not to be caught, but to cause a program to abort. It is not usual (nor, in my opinion, usually correct) to handle runtime exceptions routinely. All such should be prevented, not handled. If you want to insist that an exception be caught and handled, use a checked exception, one that is a subtype of 'Exception' but not 'RuntimeException'. Read the tutorials on java.sun.com and other introductory material on exceptions for more information. -- Lew
|
Next
|
Last
Pages: 1 2 3 4 Prev: Java crash Next: launch the default application for a given file from Java? |