Unhandled exception type InterruptedException : Java Threads


Posted on : Fri, 31st Jul 15 05:29 am UTC
100+ votes

If you get "Unhandled exception type InterruptedException" in your java programing that contains threads then you must surround the code with a try/catch block that handles InterruptedException.

InterruptedException is a checked exception, which is used to indicate the current thread that it has been interrupted by an some other thread while it was doing some operation.



Advertisements





File : InterruptedExceptionEx.java Language : Java
package com.code2care.threadsexamples;

public class InterruptedExceptionEx implements Runnable {


	@Override
	public void run() {
	
	
		for(int i = 0;i<=10;i++) {
			
			System.out.println("Runnable Tread : "+ i);
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
		}
	}

}

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +