Understand Java Exception & The effect to Java Program (Single & Multi Thread)

In the tutorial, JavaSampleApproach will introduce Java Exception and its effect to program’s execution in Single & Multi Thread Program.
Related post:
Java 7 – try-with-resources Statement
How to handle Java Exception

I. Java Exception

Java Exception is a problem when executing a code flow and they can stop the execution if not handled carefully. There are 2 kinds of the Exception: CheckedException & UnCheckedException.

1. Java CheckedException

CheckedException is an exception that can be detected by Java compiler. Editors can help developers to detect by highlight them at development time. CheckedException extends java.lang.Exception, some common CheckedException: FileNotFoundException, IOException


When compile it, there are 3 exceptions:

Eclipse Editor for detecting CheckedException:
Java Exception - Checked Exception Eclipse

With CheckedException, developers must handle it by try/catch statement or throws keyword.

Use try/catch:

or use throws keyword:

2. Java UncheckedException

UncheckedException is a kind of Exception that occurs at the executing time, so it can be called: RunTime Excepion. UncheckedException does not be detected by compiler because they are related with logic code at runtime.
UncheckedException extends from java.lang.RuntimeException, some common UncheckedException like: NullPointerException, IndexOutOfBoundsException, IllegalArgumentException, ArithmeticException.

Sample java.lang.ArithmeticException: / by zero

Exception when executing:

Runtime Exceptions
are big problems so Developers need to handle it carefully by try/catch statement or throws/throw keyword.

II. Effect of Exceptions to Program Execution

With CheckedException are easy to detected and handled by helping of Java Compiler and Editors. But UncheckedExceptions are very risk, it can stop Program. So Developers need to understand the impact of it in deeply way.

1. Java RunTime Exception in Single Thread Program

Once again with sample java.lang.ArithmeticException: / by zero

The Exception is happened at int m = i / k;, and the program will be terminated right after it. try/catch statement will help program to handle the exception then continue processing remain codes.

The exception is happened at point: m = i / k;, but The Program uses try/catch to handle it so the program continues processing with code segment: System.out.println(m);.

2. Java RunTime Exception in Multi-Thread Program

Java Exception only affects to a current Thread (the executing flow that causes exception).
Let go with below program has 2 child threads inside, both of them make exception but just only once handling exception:


The java.lang.ArithmeticException: / by zero terminates threadNotHandleException thread. But it does not impact to the execution of main thread and ThreadWithHandleException thread.

By grokonez | February 14, 2017.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *