Article

Understanding exception hierarchy in Java And Thread Life Cycle, Thread States in Java

Topic: Success CoachingPublished January 12, 2018

Legacy signals

Legacy popularity: 1,038 legacy views

Understanding exception hierarchy in Java And Thread Life Cycle, Thread States in Java It starts with showing the Java class structure for handling errors and exceptions. We will then look at three major types of exceptions in Java which are direct sub-classes of java.lang.Exception, java.lang.RuntimeException and java.lang.Error respectively, along with examples for each type.rnImportant classes of Java Exception HierarchyrnAbove class diagram depicts the 4 fundamental classes which are at the heart of exception handling in Java – • java.lang.Throwable is at the root of Java’s exception hierarchy. All types of exception are descendants of Throwable. • java.lang.Exception is direct sub-class of Throwable. Exception is the root class for all checked exceptions in Java. • java.lang.RuntimeException is direct sub-class of Exception. RuntimeException is the root class for all unchecked exceptions in Java. • java.lang.Error is direct sub-class of Throwable and represents unrecoverable JVM errors.rnThree main types of exceptions in JavarnFrom a programming perspective you will rarely work with Throwable class. Rather, it is considered a good programming practice to avoid using Throwable directly in your code. The remaining three classes – Exception, RuntimeException and Error represent the three important types of exceptions in Java.rnLet us take a look at the 3 main types of Exceptions in Java- • Exceptions – Also known as checked exceptions, these are the sub-classes of java.lang.Exception class. Whenever a piece of code is likely to throw a checked exception, then either it must be caught using a try-catch block or the signature of the method containing such code should specify this exception in the throws clause.rnExamples– FileNotFoundException, IOException. • Runtime Exceptions – Also known as unchecked exceptions, these are the sub-classes of java.lang.RuntimeException. These exceptions are specified for scenarios which neither need to be specifically caught using try-catch block nor do the methods which can throw them need to include these exceptions in the throws clause.rnExamples– ArrayIndexOutOfBoundsException, NullPointerException. • Errors – Errors are sub-classes of java.lang.Error class. These exceptions are thrown by JVM when it encounters severe issues such as if it runs out of memory and similar serious error conditions. These errors should not be caught and should be dealt with on priority as they may lead to significant glitches in deployed code.rnExamples– OutOfMemoryError, StackOverflowError.rnSummaryrnIn this article we first looked at the exception hierarchy in Java described via its class diagram.This was followed by brief explanation of the important classes in the diagram. Finally, we looked the three main types of exceptions in Java – Exceptions, Runtime Exceptions and Errors and understood their individual purpose.rnJava Thread Life Cyclern Let us start by getting a high level understanding of the 6 thread states in Java with the diagram shown next – • New – A newly created thread object instance on which the start() method has not yet been invoked is in the new state. To learn how to instantiate threads in the proper way check out this • Runnable – A thread in new state enters the runnable state when the Thread.start() method is invoked on it. There are 2 important points to note regarding the runnable state – rn1. Although the thread enters the runnable state immediately on invoking the start() method, but it is not necessary that the thread immediately starts executing. A thread runs when the logic it holds in its run() method can be executed by the processor. In case the thread logic needs any resource which is not available then the thread waits for the resource to become available.rn2. Secondly, a thread in runnable state may run for some time and then get blocked for a monitor lock, or enter the waiting/timed_waiting states as it waits for the opportunity/time to enter runnable state again. • Blocked – A running thread may enter the blocked state as it waits for a monitor lock to be freed. It may also be blocked as it waits to reenter a monitor lock after being asked to wait using the Thread.wait() method. • Waiting – A thread enters the waiting state when it is made to wait for a go-ahead signal to proceed. The go-ahead in this case is given by another thread and can be given in the following 3 scenarios – rn1. Thread waiting due to Thread.wait() method being called on it: The other thread can use Thread.notify() or Thread.notifyAll() to give the go-ahead to the waiting thread.rn2. Thread waiting as it itself has asked for joining another thread using Thread.join(): The waiting thread gets a go-ahead when the thread its waiting for ends.rn3. Thread waiting due to LockSupport.park()method being invoked on it: The waiting thread resumes when LockSupport.unPark() is called with the parked thread object as the parameter. • Timed_Waiting – A thread which is waiting as it has been specifically ‘instructed’ to wait for a specified waiting time is in a timed_waiting state. A thread can be made to wait for a pre-determined amount of time in the following ways – rn1. Thread made to wait using Thread.sleep() method.rn2. Threads being asked to wait for a permit for a specified amount of time using LockSuport.parkNanos() and LockSupport.parkUntil() methods.rn3. Threads being made to wait for a fixed amount of time using Thread.wait(long millis) or Thread.join(long millis, int nanos). • Terminated – A thread enters its ‘final resting’ state or terminated state when it has finished executing the logic specified in its run() method.

Further reading

Further Reading

4 total

Article

In today’s highly regulated and technology-driven environment, accurate monitoring of temperature and humidity has become a necessity rather than an option. From hospitals and pharmacies to data centers and food storage facilities, even minor fluctuations can lead to costly losses, safety risks, and compliance violations. This is where a dependable Humidity Monitor and robust equipment temperature monitoring system play a vital role. Humidity levels directly affect the perf

February 12, 2026

Article

For dental professionals, managing a busy practice while keeping up with accounting tasks can be overwhelming. From handling payroll and patient billing to tracking expenses and staying compliant with tax laws, dentists require precise and efficient financial support. That’s where Ask For CPA steps in with tailored accounting services for dentists and dependable bookkeeping services in Colorado, this firm helps dental practices streamline their finances and focus on deliver

January 10, 2026

Article

In the fast-evolving world of renewable energy, commercial and industrial solar installations are gaining momentum. These large-scale projects promise long-term savings, energy independence, and a reduced carbon footprint for businesses and institutions. But before a single panel is installed, there’s a crucial step that determines the success of these projects—appointment setting. Unlike residential solar projects, where homeowners are usually the sole decision-makers an

April 18, 2025

Article

The solar industry is booming, but with more competition than ever, companies need to refine their approach to securing and converting leads. One of the most critical yet often overlooked aspects of the sales process is appointment setting. Without a steady flow of qualified solar appointments, even the best products and pitches can fall flat. Recently, Solaralm hosted an insightful webinar focused on helping solar businesses maximize their sales through effective appointment

April 14, 2025