JVM vs Programmatic Exceptions

Anna Scott
4 min readSep 24, 2021

There is a number of thing that might go wrong in a program. Java exceptions is the way java program deals with things if something goes wrong. Java exceptions can be classified into checked and unchecked, however in this post we will talk about JVM (java virtual machine) exceptions and programmatic exception.

JVM exceptions are called like that because they are thrown by JVM itself. Programmatic exceptions are thrown using the “throw” clause. It seems pretty straight forward, right? However, let’s look at the exceptions hierarchy:

Class Throwable is the superclass for all exceptions and errors in JAVA language. The documentation for the Throwable class tells us that: “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 the Java throw statement.”

As you can see we can throw and catch all the exceptions and errors programmatically. Take a look at the code that throws and catches an error:

public static void main(String... input) {
try {
throw new StackOverflowError();
} catch (StackOverflowError e) {
System.out.println("Throwing and catching errors pragramatically is not a good idea!");
}
}

I am doing this purely for demonstration purposes, it is highly advisable against.

The output:

Throwing and catching errors pragramatically is not a good idea!

The compiler does not complain if we just throw an error without catching. The following code compiles just fine:

public static void main(String... input) {
throw new StackOverflowError();
}

From documentation: “An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.”

JVM handles errors.

Let’s look into checked exceptions. Here we have IOException:

public static void main (String[] args) {
try {
throw new IOException();
} catch (IOException e) {
System.out.println("I caught a checked exception");
}
}

The result is:

I caught a checked exception.

The compiler forces us to programmatically handle checked exception. If we try the following:

public static void main (String[] args) {
throw new IOException();
}

our code does not compile. For the most part checked exceptions have environmental causes that could be resolved and are not fatal to JVM, so we can handle them programmatically.

We can categorize all checked exceptions as programmatic.

Though compiler does not insist on us handling unchecked exception programmatically, we can:

public static void main(String... input) {
int a = 1;
int b = 0;
try {
int result = a / b;
} catch (ArithmeticException e) {
System.out.println("Please, no division by zero");
}
}

The result is:

Please, no division by zero.

The following code has no problem compiling:

public static void main(String... input) {
int a = 1;
int b = 0;
int result = a / b;
}

Who should handle unchecked exceptions — programmer or JVM?

Most unchecked exceptions are caused by bugs in the code, and we should the JVM handle them, because JVM messages will help us fix our code. Unchecked exceptions that guard us from from some wrong input should be handled programmatically. For example, if we expect positive integer we can programatically throw IllegalArgumentException:

ublic static void main(String[] args) {
int n = -1;
try {
if (n < 0) throw new IllegalArgumentException();
System.out.println(n);
} catch (IllegalArgumentException e) {
System.out.println("n can't be negative");
}
}

The output:

n can’t be negative

Or if we are expecting string that could be converted to integer we can programmatically throw NumberFormatException:

public static void main(String... input) {
String numberString = "Cats and dogs";
int number;
try {
number = Integer.parseInt(numberString);
} catch (NumberFormatException e) {
System.out.println("We can not convert this string to integer" );
}
}

The output:

We can not convert this string to integer

JVM handles errors. Checked exceptions are handled programmatically. JVM handles most of unchecked exceptions, unless we need programmatically enforce certain restriction on passed values like we saw in the examples of IllegalArgumentException and NumberFormatException above.

You can find all code here:

https://github.com/forfireonly/MediumArticlesJava/blob/master/src/com/company/LearningExceptions.java

Happy coding my friends!

--

--