adsense


Showing posts with label JAVA EXAMPLES EXCEPTION WITH THREAD. Show all posts
Showing posts with label JAVA EXAMPLES EXCEPTION WITH THREAD. Show all posts

Thursday, 29 October 2015

JAVA EXAMPLES EXCEPTION WITH THREAD PAGE 135

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES EXCEPTION WITH THREAD






Java Examples - Exception with thread



Problem Description:
How to use exceptions with thread?
Solution:
This example shows how to handle the exception while dealing with threads.
class MyThread extends Thread{
   public void run(){
      System.out.println("Throwing in " +"MyThread");
      throw new RuntimeException();
   }
}
class Main {
   public static void main(String[] args){
      MyThread t = new MyThread();
      t.start();
      try{
         Thread.sleep(1000);
      }
      catch (Exception x){
         System.out.println("Caught it" + x);
      }
      System.out.println("Exiting main");
   }
}
Result:
The above code sample will produce the following result.
Throwing in MyThread
Exception in thread "Thread-0" java.lang.RuntimeException
        at testapp.MyThread.run(Main.java:19)
Exiting main