Exceptions are the events that occur due to the programmer error or machine error which causes a disturbance in the normal flow of execution of the program. When a method encounters an abnormal condition that it can not handle, an exception is thrown as an exception statement. Exceptions are caught by handlers(here catch block). Exceptions are caught by handlers positioned along with the thread’s method invocation stack. If the calling method is not prepared to catch the exception, it throws the exception up to its calling method and so on. So in the java program exception handlers should be positioned strategically, so the program catches all the exception from which the program want to recover.
You are watching: Exception in thread "main" java.lang.error: unresolved compilation problem:
Lifecycle of a thread: The class implements a Thread class or Runnable interface then the extended class has start() method run the thread, sleep() methods cause the currently executing thread to sleep for the specified number of milliseconds, and many more.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Prior to discussing the approaches, state. transactions of thread should be known to further deal with exceptions for better understanding. A thread in Java at any point in time exists in any one of the following states. A thread lies only in one of the shown states at any instant:
1. A class name RunnableThreadimplements the Runnable interface whichgives the run( ) methodexecuted by the thread. The object of this class is now runnable2. The Thread constructor is used to create an object of RunnableThread class by passing the runnable object as a parameter.3. The start() method is invoked on the Thread object as it returns immediately oncea thread has been spawned.4. The thread ends when the run( ) method ends which is to be normal termination or caught exception.5. Now in order to create a new threadrunner = new Thread(this,threadName) ;6. In order to start the new thread.runner. start() ;7. public void run( ) is an overrideable method used to display the information of a particular thread.
See more: Regal Cinemas Macarthur Marketplace 16 Irving Tx, Regal Cinemas Macarthur Marketplace 16
Thread.currentThread().sleep(2000) is used to deactivate the thread until the next thread started execution or used to delay the current thread.Uncaught exception handler will be used to demonstrate the use of exception with thread. It is a specific interface provided by Java to handle exception in the thread run method.There are two methods to create a thread:Extend the thread Class (java.lang.thread)Implement Runnable Interface (java.lang.thread)1. Exception and Exception handling with threadsHere, a new thread is created in the class which is extending the thread class in which run() method is overridden. This invokes the entry point of the new thread created in the class which was extending the thread class. Further, start() method is used to start and run the thread in the program.