In my eclipse plugin I use a ScheduledExecutorService for a repeating task. However this seems to lead to some unreachable code within the scheduled task because I can set a breakpoint in eclipse up to a certain line and it will be reached in the debugger but when I set it one line further it is not reached... Just nothing happens then, no exception just nothing.
When I try to overstep this respective line I land somewhere in the sources of the ScheduledThreadPoolExecutor and my stack shows this:
ScheduledThreadPoolExecutor$ScheduledFutureTask<V>(FutureTask<V>).run() line: not available [local variables unavailable]
Whats going on here?
Okay the problem was that there was actually an exception being thrown but it seems like the ScheduledExecutorService swallows it without telling anything about it...
I foud this out by surrounding my code in the run-method with a generic try-catch-block like this:
#Override
public void run() {
try {
// Code
} catch (Exception e) {
e.printStackTrace();
}
}
Related
I have a java project which does not output the same result every time it is ran and object variables are changed upon each call of the starting method in main, resulting in a case where an errors are not thrown for some runs of the program, but sometimes an error may pop up in a niche case. This has led me to running the program over and over until some exception may be thrown.
Is there a way in Eclipse to run main over and over x amount of times until it encounters (or doesnt!) a caught error?
You can use the try catch keywords.
public static void main(String[] args) {
try {
// do something
}
// exits system if encounters any exception
catch (Exception e) {
System.exit(0);
}
}
If you wish to run the main method a specific number of times, you can put all your method inside the try statement and then inside a for loop. If you are looking for a specific exception, you can change the word "Exception" to your desired exception. For example:
try {
// do something
}
// exits system if encounters any exception
catch (ArrayIndexOutOfBoundsException e) {
System.exit(0);
}
You can put as many catch statements as you would like for each of the desired exceptions
For the runnable Jar programs I create, I can read the unhandled exceptions if I run it from the command prompt (java -jar myprogram.jar).
Example code:
public static void main(String args[]) {
try {
EntireProgram();
} catch (Throwable t) {
throw new Error("somehow our error handling code has errors");
}
}
public static void EntireProgram() {
System.out.println(1 / 0);
}
Command line output:
However, if I run the Jar program by double-clicking it (expected of most end-users), the unhandled exceptions stay silent. I've noticed that this is in contrast to "native Windows programs" which I believe have unhandled exceptions handled by the OS default handler (modal dialog message):
The advantage of handling unhandled exceptions this way is that the user will know something wrong has happened (and perhaps report it, hopefully).
How can I make my program's unhandled exceptions be handled in such a way?
Why don't you add a single line like this in your catch block ..which will show you the error message irrespective of How you run your app.
javax.swing.JOptionPane.showMessageDialog(null,"Something is really wrong..Hence notifying you..");
Ok, so im super, extra, brand new to java coding, but im slowly getting all the terms. But the real issue is this error. I'm trying to do a little coding snippet that, when started, will open a batch file (i'm much more experienced with them). I searched very much for answers, but nothing solved it, so i had o post about my specific situation.
This is said code:
public class Startbat {
public static void main(String[] args){
try {
Runtime.getRuntime().exec("cmd /c start hello.bat");
}
catch (IOException) {
System.out.println ("Something is wrong here...");
}
}
}
and when i try to javac it, i get this:
C:\Users\Owner>javac -g "C:\Users\Owner\Desktop\codes\codes\Startbat.java"
C:\Users\Owner\Desktop\codes\codes\Startbat.java:6: error: <identifier> expected
catch (IOException) {
^
1 error
I have found that the error I get is about the catch line/block not being in a method, when I have definitely made sure that it IS within the main.
I had a few error with the exception there, but i guess i solved it. i don't know what the exception would have been ((meaning how would that IOException occur from the runtime.exec stuff) meaning does that mean it won't start the batch file if it does get to work?).
Any help would be appreciated, even if its just "This is impossible to do"
You need to specify a variable for the exception, so you can use it later, for example...
catch (IOException exp) {
You should also consider printing the exception, at least to the console if not some kind of logger...
catch (IOException exp) {
ex.printStackTrace();
//...
This will make it easier to track down potential problems if/when they occur.
As a side note, you really should use ProcessBuilder instead of Runtime.exec, apart from anything else, it provides better management of the output streams and agruments that contain spaces...
I'm very confused with handling errors on runnables passed to a threadpool.
I am doing everything by the book (literally, I'm adhering to Goetz' "Concurrency in Practice") and I'm not capturing any errors that Eclipse is showing me, and I'm getting weird NoClassDefFoundErrors even though my buildpath looks fine.
I tried killing my local repo and recloning it to get a fresh project build, but still getting the error. And none of my try-catches or System.out.println's are working either. Is there any way Eclipse can give me a better view of the error? Or a way to actually get the error handler's to work?
//ThreadPool and runnable that is failing, even with error captures
MyPriorityThreadPool.execute(new RunWithPriority(priority) {
Throwable thrown = null;
#Override
public void run() {
try {
batch.buildData();
} catch (Exception e) {
thrown = e;
}
finally {
if (thrown != null) {
thrown.printStackTrace();
}
}
}
});
}
I'm not quite sure what you're trying to accomplish, but the reason you're not catching the NoClassDefFoundError in your code is that you're catching Exception instead of Throwable or Error. Exception is more specific than Error and not what NoClassDefFoundError inherits from, so NoClassDefFoundError isn't caught by your catch expression.
I had a strange problem today... I'm going to make a simplified example since it "worth a thousands words" :D
public class Application() {
public static void main(String[] args) {
try {
A a = new A(); // this may throw exceptions
// (which will cause an ExceptionInInitializerError)
} catch (Throwable t) {
JOptionPane.showMessageDialog(null, "Oooops!");
System.exit(1);
}
}
}
Since it's a stand-alone application with a Swing GUI, my goal is to give a message to the user in case of any problems (in this case at startup)... the code above works in Eclipse IDE but when I export the project as executable jar by double-clicking on it, well, it just won't open.
So I try to execute it in cmd with java -jar application.jar and it prints in the shell that there was an ExceptionInInitializerError.
Why the error was not caught?
It doesn't work even if I specify catch (ExceptionInInitializerError e).
EDIT:
After more indepth debugging, I found out that this problem only happens when two particular exceptions occur and the latter occurs in the catch block of the former.
I corrected the bug by changing the order of some checks that I do on startup.
The problem btw should never happen since it was originated by a volountary mistake of the JDBC driver class name to load in a static block.
Well, at least it made me clearly understand why constructors and static initialization blocks should not throw exceptions: it makes debugging almost impossible in the case in which the class that throws the exception is used by many classes, since it may become very hard to find out when the class is loaded.
I can think of three possible explanations for an ExceptionInInitializerError not being caught in your example:
It could be being thrown by JOptionPane.showMessageDialog(null, "Oooops!");
It could be thrown before main is called.
It could be thrown on a different stack.
In fact, I think that the 2nd one is the most likely, as ExceptionInInitializerError is thrown when some unchecked exception is thrown (and not caught) during the initialization of a class. That could well be happening before you enter the try block.