I need to create a method such that if I enter in that method and to if takes me more then 30 seconds to process that method then it should throw an exception and I will immediately get out from that method and I can handle that exception in the calling method so that my next process goes fine.
public static void method() {
Timer timer=new Timer();
timer.schedule(new TimerTask() {
@SuppressWarnings("finally")
@Override
public void run() {
try {
System.out.println("inner "+Thread.currentThread());
System.out.println("first ");
}
finally{ System.out.println("before return "); throw new RuntimeException("Sorry TimeOut");}
}
},400);
try{
System.out.println(Thread.currentThread());
Thread.sleep(1000);
}catch(InterruptedException e){}
System.out.println("OKKKKK");
//return null;
}