Dreamer
Dreamer

Reputation: 7551

JVM and programmatic exceptions

Can I say JVM exceptions are unchecked exceptions and Programmatic exceptions are checked exceptions? Because it seems like JVM exceptions are thrown at runtime so could not be checked...

Thanks

Upvotes: 0

Views: 3122

Answers (3)

Stephen C
Stephen C

Reputation: 718708

Can I say JVM exceptions are unchecked exceptions and Programmatic exceptions are checked exceptions?

First, "JVM exceptions" is not standard terminology. (And the fact SCJP6 talks about exceptions that are thrown by the JVM doesn't make your terminology correct. The SCJP6 text that you quoted is using the words descriptively ... not as an attempt to define terminology.)

The answer to your question is No. Many "programmatic" exceptions are unchecked. For instance, NumberFormatException is unchecked.

Because it seems like JVM exceptions are thrown at runtime so could not be checked...

That logic doesn't make sense either. All exceptions are thrown at runtime. This is not what "checked" versus "unchecked" means. The real distinction between checked and unchecked exceptions is that checked exceptions must be either caught or declared in the method signature. With unchecked exceptions you don't have to do either.

Now it so happens that all exceptions that are thrown by the JVM itself ARE unchecked. But that is more about the nature of the "events" that trigger the exceptions. They are typically the result of either a bug in some Java code, or a non-recoverable JVM failure. These are not the kind of thing that an application typically recovers from, so it making them unchecked simply avoids some unnecessary verbiage (or worse) in the source code.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200138

There may be two separate distinctions involved in what you are asking. Exception thrown by the JVM runtime extend from Error whereas programmatic exceptions extend from Exception. Unchecked programmatic exceptions extend from RuntimeException. So there are two distinct class hierarchies for unchecked exceptions. Do note that the JVM/programmatic distinction is convention only: you can indeed throw any exception class you want from Java code.

Also make sure to distinguish JVM exceptions from JDK exceptions—those thrown by the standard Java library. No JVM exception is checked, but a lot of JDK exceptions are.

As for the checked/unchecked distinction, the compiler forces you to declare in advance all checked exceptions your method may throw either explicitly or by calling a method that may throw them. No such checking is done for unchecked exceptions.

Upvotes: 3

sstendal
sstendal

Reputation: 3218

By JVM exception you probably think about exceptions thrown by the Java VM, while Programmatic exceptions are exceptions that are thrown by the java programmers code.

Most exceptions thrown by the Java VM is actually thrown by java code in the standard libraries in the Java VM. You can download this java code and you will see that the libraries throws exceptions in the same way as a programmer that writes a java program. A standard java programmer can also throw the same exceptions in the same way.

The Java VM may also throw Java Errors. An Error is not an Exception, but can be thrown and catched in the same way as Exceptions. Only the VM should throw an Error.

Upvotes: 1

Related Questions