Exception at calling one program from another program inside Runnable interface - java

I am facing problem while I am trying to call another program from Task3 program and try to execute it on scheduled time. Getting exception at line 14 as below.
Please let me know where I am going wrong in call program from run method of Runnable interface.
Task3.java:14: error: unreported exception Exception; must be caught or declar
to be thrown
Mult.main(new String[0]);
import java.util.Timer;
import java.util.TimerTask;
public class Task3 {
public static void main(String[] args) {
TimerTask task = new TimerTask() {
#Override
public void run() {
// task to run goes here
System.out.println("Hello !!!");
Mult.main(new String[0]);
}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1 * 1000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay,
intevalPeriod);
} // end of main
}

Mult.main has a throws clause that includes a checked exception, so for the code to be accepted by the compiler you'd need to add that exception to the throws clause of the run method or catch the exception. You can't add that exception to the throws clause however, since you override TimerTask.run, which declares no exceptions.
The only remaining opitions are catching that error or change Mult.main to not throw any exceptions that are not subclasses of RuntimeException. You could e.g. catch the exception and throw a runtime exception or do something else, if an exception occurs:
TimerTask task = new TimerTask() {
#Override
public void run() {
// task to run goes here
System.out.println("Hello !!!");
try {
Mult.main(new String[0]);
} catch (Exception ex) {
// handle the exception,
// in this case by throwing a RuntimeException with ex as cause
throw new IllegalStateException("I didn't expect a exception.", ex);
}
}
};

This is a compile-time error. Your method Mult.main() can throw an Exception.
Surround it with try/catch to handle error, e.g.
try {
Mult.main(new String[0]);
} catch (Exception e) {
// Handle your error here
}

It's hard to tell without more code, but it looks like:
Mult.main(...) is defined as throwing an exception, in which case the code that calls must handle the exception.
Either:
Wrap the call to Mult.main(...) in a try-catch block, or
Define Task3.main(...) to throw Exception as well
P.S. or maybe it's timer.scheduleAtFixedRate(...), can't really tell without line numbers

Related

java Catch exception inside a async callback

I have a callback which may throw a custom exception.
I'm trying to throw it, but it's not being catched on the outer scope, nor the compiler let me catch it, it says: "Exception is never thrown is the corresponding try block", even though it is.
this is my code:
public void openAsync(MessageAsyncCallback callback) {
try {
this.sendChannelOpen(this.getChannel(), getChannelOpenData().getFlags(), new MessageAsyncCallback() {
#Override
public void onComplete() throws NanoException {
// INanoPacket message = transport.getMessageByClassName(AudioServerHandshake.class.getName());
INanoPacket message = transport.getMessageByClassName(AudioClientHandshake.class.getName());
Log.info("Got audio server handshake, trying to client-handshake it");
sendClientHandshakeAsync((AudioServerHandshake) message, callback);
}
});
} catch (NanoException e) {
System.exit(-2);
}
}
and it doesn't let me catch NanoException
EDIT:
inside transport.getMessageByClassName I throw a NanoException.
EDIT2:
this is the method who invokes the exception:
public INanoPacket getMessageByClassName(String destClassName) throws NanoException {//} throws NanoException {
long startTime = System.currentTimeMillis(); // fetch starting time
INanoPacket message = this.getMessageFromTCPQueue();
while (!(message.getClass().getName().equals(destClassName)) && isRuntimeValid(startTime)) {
this.insertToTCPQueue(message); // put message back in queue
message = this.getMessageFromTCPQueue();
}
if (!(message.getClass().getName().equals(destClassName))) {
// timeout...
throw new NanoException("Couldn't find destination message: " + destClassName);
}
return message;
}
and I want to handle the exception not even in openAsync but on the method that calls openAsync.
why? because I'm handling messages coming from a remote device, this is why it's async. and I'm using some kind of timeout to wait for a specific message, and if the message isn't coming I want to restart the whole program.
Please notice that in your code you are not invoking onComplete method, you are defining it.
The exception would be thrown in a separate part of the code, possibly separate Thread (as it seems to be async). Therefore the "Exception is never thrown is the corresponding try block" message is right, as the exception will never be thrown when invoking this.sendChannelOpen(...) method.
Your try-catch statement needs to wrap the place where you invoke the onComplete method. As only by invoking onComplete method can you expect NanoException.
EDIT based on comments:
If you need to handle the exception throw in getMessageByClassName you can do it in onComplete method and not rethrow it. If you want to handle it somewhere else, you'd need to provide us the code of sendChannelOpen method or a place where the callback is invoked.
EDIT2 (based on question edits):
Please see the code below, as an example of how you can communicate between threads. I've used Latch, but there are other classes in java.util.concurrent that you may find useful.
BTW, I'm not going into the discussion why you want to restart the whole app on your NanoException, although there might be other options worth considering for recovering from that Exception.
import java.util.concurrent.CountDownLatch;
class NanoException extends Exception {}
interface MessageAsyncCallback {
void onComplete() throws NanoException;
}
public class AsyncApp {
private static final CountDownLatch errorLatch = new CountDownLatch(1);
public static void main(String[] args) {
new AsyncApp().run();
}
void run() {
sendChannelOpen("something", new MessageAsyncCallback() {
#Override
public void onComplete() throws NanoException {
// the whole try-catch-sleep is not really needed, just to wait a bit before exception is thrown
try {
// not needed, just to wait a bit before exception is thrown
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new NanoException();
}
throw new NanoException();
}
});
try {
System.out.println("This is a main thread and we wait here, while the other thread executes...");
errorLatch.await();
System.out.println("Latch has reached 0, will now exit.");
System.exit(-2);
} catch (InterruptedException e) {
System.out.println("Error in main thread.");
System.exit(-1);
}
}
void sendChannelOpen(String notImportant, MessageAsyncCallback troublesomeCallback) {
runSomethingInSeparateThread(troublesomeCallback);
}
void runSomethingInSeparateThread(MessageAsyncCallback troublesomeCallback) {
new Thread(() -> {
try {
troublesomeCallback.onComplete();
} catch (NanoException e) {
System.out.println("You can catch it here, and do system exit here or synchronize with main Thread as below");
errorLatch.countDown();
}
}).start();
}
}

JUnit test case for Java Runnable run

I want my program to run fine even though there is an exception. The following does that. Can someone help me writing JUnit test case for this?
protected static Runnable myMethod=new Runnable() {
#Override
public void run() {
try {
//my code - may raise exception
} catch (Throwable t) {
logger.error("Exception occured", t.getMessage());
}
}
};
Throwing an exception up from a #Test method will cause it to end with an error, which isn't a success. So the textbook approach for such cases is to just set up the conditions, run the method, and assume everything is OK if an exception doesn't cause the test to error:
#Test
public void testMyLogic() {
// Set up conditions that would cause an the Runnable's body to throw an exception
myMethod.run();
// If we got here an exception was NOT thrown.
// Implicitly, we're OK.
}

Java convert from one exception to another

I have a common project with some shared code that is being used in another project. I'm trying to convert/map the exception from the common project CommonException to a new type of Exception let's call it SuperAwesomeException.
The aim is to have a generic way of handling all custom exceptions in the project.
I've attempted to do this using an UncaughtExceptionHandler. This seems to work when running the project but not from within JUnit, since that wraps each test in a try/catch block as described here.
public final class ExceptionHandler implements Thread.UncaughtExceptionHandler {
#Override
public void uncaughtException(Thread thread, Throwable exception) {
if (exception instanceof CommonException) {
throw new SuperAwesomeException(exception.getMessage());
}
if (exception instanceof SuperAwesomeException) {
throw new CommonException(exception.getMessage());
}
else {
System.out.println("ERROR! caught some other exception I don't really care about");
System.out.println("Not doing anything");
}
}
}
Is there another way I can map from one Exception to another or can I somehow tell JUnit not to catch certain exceptions and check the Exception is mapped to the correct one?
UPDATE - How I initially tried to write the Test:
public class ClassThatThrowsException {
ClassThatThrowsException() {
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
}
public void doSomething() {
throw new CommonException("Something boring blew up!");
}
}
public class ClassThatThrowsExceptionTest {
#Test(expected=SuperAwesomeException.class)
public void testAwesome() {
ClassThatThrowsException c = new ClassThatThrowsException();
c.doSomething();
}
}
which throws:
java.lang.Exception: Unexpected exception, expected<SuperAwesomeException> but was<CommonException>
The problem is: when you are using JUnit, the framework will catch your exception. Therefore the uncaught exception handler isn't called in the first place!
See here for more details.
Thus, you have to do two things:
A) write tests that make sure that your exception handler implementation works as desired
#Test(expected=SuperAwesomeException.class)
public void testAwesome() {
new ExceptionHandler().uncaughtException(null, new CommonException("whatever"));
}
B) thest the plumbing - you want to make sure that this specific uncaught handler gets actually set by your code:
#Test
public void testDefaultHandlerIsSet() {
// creating a new instance should update the handler!
new ClassThatThrowsException();
Thread.UncaughtExceptionHandler handler = Thread.getDefaultUncaughtExceptionHandler();
assertThat(handler, not(nullValue()));
assertThat(handler, instanceOf(ExceptionHandler.class));
}
Finally - please note: you should not just do new XException(oldException.getMessage). Rather go for new XException("some message, oldException).
In other words: you got a cause here; so you better use the incoming exception as cause within the new one you intend to throw. Otherwise you loose all stack trace information.

Java TimerTask wants variable to be final

Anyway so im trying to make something like a chat program and someone told me to use this code to check for new messages while allowing the user to submit a message:
timer.schedule(new TimerTask() {
#Override
public void run() {
read.readChat(line);
}
}, 0, 1000);
//Wait for user input
while(true) {
String bar = scan.next();
}
Where the read.readChat(line); is the method which displays the messages from another file. Java tells me that read and line both have to be declared as final... I dont understand why especially for the "line" because that's a variable and I need it to change.
Additionally after I declare them as final I get this error:
unreported exception java.lang.Exception; must be caught or declared to be thrown
read.readChat(salt);
What am I doing wrong?
Second error tells you that the method read.readChat(line); throws a checked exception so you have to catch.
You cannot use local variables declared at outer class in the Anonymous inner classes. Make them final or declare them as fields (instance variables).
EDIT:
#Override
public void run() {
try
{
read.readChat(line);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
Cody, which thread do you want that exception to be thrown to? Right now the timer task is going to run in another thread (Timer's). So, is the timer thread that is going to deal with it in your current design. If you just want the run to re-throw an exception, you can wrap it to a RuntimeException.
#Override
public void run() {
try {
read.readChat(line);
}catch(RuntimeException ex) {
throw ex;
}catch(Exception ex) {
throw new RuntimeException(ex);
}
}
But, for the Timer it is not going to change anything because the run() method is the last thing the Timer thread is going to see. If you need advanced an error handling, you have to deal with it inside run(). Can you tell a bit more about what you are trying to do?

How to simulate an unhandled exception in Java

I am creating some multi-threaded code, and I have created a JobDispatcher class that creates threads. I want this object to handle any unhandled exceptions in the worker threads, and so I am using
Thread.setUncaughtExceptionHandler(this);
Now, I would like to test this functionality - how can I generate an unhandled exception in the run() method of my worker object?
Just throw any exception.
E.g.:
throw new RuntimeException("Testing unhandled exception processing.");
Complete:
public class RuntimeTest
{
public static void main(String[] a)
{
Thread t = new Thread()
{
public void run()
{
throw new RuntimeException("Testing unhandled exception processing.");
}
};
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
public void uncaughtException(Thread t, Throwable e)
{
System.err.println(t + "; " + e);
}
});
t.start();
}
}
What's the problem with just throwing an exception:
throw new Exception("This should be unhandled");
Inside your run method. And of course, not catching it. It should trigger your handler.
You should throw some unchecked exception. An unchecked exception does not require your code to handle it, and is therefore a good candidate to make all the way down the call stack.
You can choose RuntimeException for example, or even something like AssertionError, if you want to minimize the chances that some part of the code catches the exception and handles it before it reaches your handler.
just add this code and you'll get unhandled exception without lint error:
int i = 1/0;

Categories

Resources