How to distinguish between Programmer and JVM exceptions



As the title shows, how can I specify a JVM exception thrown from a Programmatically (does it mean, is called by the programmer or program) thrown exception ?


JVM Exceptions


1) ArrayIndexOutOfBoundsException

2) ClassCastException

3) NullPointerException



Software Selected


1) NumberFormatException

2) AssertionError


Many thanks

+7
java exception
source share
3 answers

I'm not sure what you mean by JVM exceptions. These are all runtime exceptions that can be programmed by the programmer anywhere (an AssertionError exception), although it is considered a bad style to throw certain exceptions, such as a NullPointerException . The fact is that there is not a single quality that separates the two categories that you mention, in addition to their typical use. All runtime exceptions are thrown directly or indirectly by a RuntimeException .

From JavaDocs for Throwable :

Only objects that are instances of this class (or one of its subclasses) are thrown by the Java virtual machine or can be thrown by a Java assertion.

Since the same superclass defines all exceptions thrown by either the JVM or the programmer, you cannot easily distinguish them.

+4
source share

You cannot do this statically because there is no such difference.

Any exception defined in the standard Java class libraries may be caused by software or third-party library code. In some cases, it is a bad (or even terrible) idea to throw a standard exception, but in others it is recommended to do so.

The only possible way to distinguish between the exception thrown by the JVM and the application code is to examine the stack frames from the generated exception to find out which class threw the exception. (Strictly speaking, this does not tell you where the exception was thrown ... but it is close enough, given that the exceptions are almost always thrown and thrown in the same expression.)

But even this is not particularly useful. I mean, what is the semantic difference between the exception generated by the application code and the class library? Of course, he does not say anything about the root cause of the problem.

+5
source share

I don’t think you will find a complete list, since there is no clear distinction between jvm and programmer exceptions, with the exception of a few special cases:

  • Most Error classes are called by the virtual machine due to internal or external reasons. The only exception ThreadDeath throws into a thread when this thread is stopped, and is a kind of "hack" to force the thread to unwind its stack and exit.
  • the most checked exceptions are related to environmental problems that lead to a malfunction, but may be resolvable and not fatal to the JVM (IOException, SQLException, RemoteException).
  • the rest, unchecked exceptions, are a combination of both jvm and an exception initiated by the programmer. For example, the JDK throws an IllegalArgumentException when method parameters do not meet the specification. Is it a JVM exception or a software exception? It is unclear whether your JVM exception definition applies to the JDK or not. ArrayIndexOutOfBounds generated for illegal access to the array generated by the JVM, but it is also added to some apis, for example. Track.get from java.midi. (Although this could be argued as bad form, the superclass IndexOutOfBounds should have been used instead.)
+3
source share

All Articles