Exceptions in Java • Exception Handling in Java is one of the effective means to handle the runtime errors so that the regular flow of the application can be preserved. Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. • Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions. Exceptions can be caught and handled by the program. When an exception occurs within a method, it creates an object. This object is called the exception object. It contains information about the exception, such as the name and description of the exception and the state of the program when the exception occurred. Major reasons why an exception occurs • Invalid user input • Device failure • Loss of network connection • Physical limitations (out of disk memory) • Code errors • Opening an unavailable file Errors • Errors represent irrecoverable conditions such as Java virtual machine (JVM) running out of memory, memory leaks, stack overflow errors, library incompatibility, infinite recursion, etc. Errors are usually beyond the control of the programmer, and we should not try to handle errors. • Error: An Error indicates a serious problem that a reasonable application should not try to catch. • Exception: Exception indicates conditions that a reasonable application might try to catch. Exception Hierarchy • All exception and error types are subclasses of class Throwable, which is the base class of the hierarchy. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception. Another branch, Error is used by the Java run-time system(JVM) to indicate errors having to do with the run- time environment itself(JRE). StackOverflowError is an example of such an error. Types of Exceptions Exceptions can be categorized in two ways: 1. Built-in Exceptions Checked Exception Unchecked Exception 2. User-Defined Exceptions Built-in Exceptions: Built-in exceptions are the exceptions that are available in Java libraries. These exceptions are suitable to explain certain error situations. • Checked Exceptions: Checked exceptions are called compile- time exceptions because these exceptions are checked at compile-time by the compiler.
• Unchecked Exceptions: The unchecked exceptions are just
opposite to the checked exceptions. The compiler will not check these exceptions at compile time. In simple words, if a program throws an unchecked exception, and even if we didn’t handle or declare it, the program would not give a compilation error. Java Exception Keywords Java Exception Example Multithreading in Java Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process. A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. LIFE CYCLE OF A THREAD The Stages Of The Life Cycle − • New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread. • Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. • Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing. • Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. • Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates. Threads can be created by using two Mechanisms :
• Extending the Thread class
• Implementing the Runnable Interface Thread creation by extending the Thread class • We create a class that extends the java.lang.Thread class. • This class overrides the run() method available in the Thread class. • A thread begins its life inside run() method. • We create an object of our new class and call start() method to start the execution of a thread. • Start() invokes the run() method on the Thread object. Thread creation by implementing the Runnable Interface • We create a new class which implements java.lang.Runnable interface • and override run() method. • Then we instantiate a Thread object and • call start() method on this object. Thread Class vs Runnable Interface • If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes. • We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface. • Using runnable will give you an object that can be shared amongst multiple threads.
Chapter 01 Computer Organization and Design, Fifth Edition: The Hardware/Software Interface (The Morgan Kaufmann Series in Computer Architecture and Design) 5th Edition
Chapter 01 Computer Organization and Design, Fifth Edition: The Hardware/Software Interface (The Morgan Kaufmann Series in Computer Architecture and Design) 5th Edition