When running our program we get an exception of type java.lang.IllegalMonitorStateException. On Java6 API website, it says there is a constructor that gives a details about the exception: IllegalMonitorStateException(String s)
How can we use this to get a better idea of where the bug is in our code? Is there anything else we can do (besides lots of debugging which we're currently doing) to pinpoint the function or line that failed?
The details must be given when the Exception is created (Constructor, right?) and if you are not creating it, there is no way for you to provide the details.
You can analize the StackTrace of the Exception. It shows the classes, methods and souce line which were called to cause the Exception.
One cause for the IllegalMonitorStateException is trying to wait on an Object without having synchronized on it. See the Javadoc.
There are other possible causes and the Exception may be thrown by some library/external code. I think only the StackTrace can help...
This is maybe occurring because the instance of the object which you are calling wait or notify on is different that the instance you synchronized with. For example:
Integer a;
a = new Integer(0);
synchronized(a) {
System.out.printf("I synchronized on %h.", a);
++a;
System.out.printf("But, I am calling notify for %h and I hold no lock for it.", a);
a.notify();
}
This will throw the IllegalMonitorStateException because the instance that 'a' points to is no longer the same.
How can we use this to get a better
idea of where the bug is in our code?
Is there anything else we can do
(besides lots of debugging which we're
currently doing) to pinpoint the
function or line that failed?
In this case, printing the message by itself probably won't help much. What you need is a stacktrace with source file names and line numbers.
Make sure that all relevant ".class" files / JARs were built with file and line number debug information included. This is the default, but compiling with "-g:none" will strip this ... as will most JAR file obfuscators.
Next, add a try / catch block to catch the IllegalMonitorStateException and either call ex.printStackTrace() or log the exception.
From the stacktrace you should be able to see what line in the code threw the exception. The chances are that is was a call to Object.wait(...) or something like that. Check the javadoc for the offending method to find out what circumstances cause the exception to be thrown.
(And once you are done, remember to move the try / catch block you added.)
You should print the stack trace, which will give you the exact location in the source.
Unfortunately it's not uncommon for the JVM to throw exceptions which contain no detail message to assist in debugging.
Related
Is there a better way than wantonly throwing an exception and capturing the stacktrace to find out why (or at least from where) my function was called?
I can log the parameters the caller is using, but I cannot get the caller location itself. It is some third party software, so finding all references and adding log information there is not an option. I also hesitate attaching a debugger to a running jvm.
You can call Thread.currentThread().getStackTrace().
This will give you an StackTrace array the same way you would get if you'd thrown an Exception.
Hope this helps. :)
It's perfectly possible to create an exception, not throw it, and inspect its stacktrace:
Exception e = new Exception();
e.printStackTrace(System.out);
Note that doing it often may have a detrimental effect on the performance of your JVM.
StackTraceElement[] stacktrace = e.getStackTrace();
The 0th element of the stacktrace array will contain the method in which the exception was instantiated.
I had a piece of code my professor commented on, saying that "this is called a squelch" and that it's a huge sin in programming.
Here's the code I had,
if (command.matches(input)) {
try {
command.execute();
} catch (UniversalException e) {
}
return;
}
The "squelch" was within the catch(). I have kind of an idea from research what this is, but if someone could explain it to me in simpler terms, I'd appreciate it. Also how to avoid a squelch would be nice, too.
Also, if it helps, that bit of code also includes
public UniversalException() {
System.out.println("The file you entered could not be located. Please try again.");
}
in another class, which is what is printed out when a user enters an invalid file name.
I don't understand any of this exception stuff well yet, and my professor simply asked for something that prints out a nice message to the user instead of a stack trace.
That's a funny, if descriptive name for it :)
If I'm not misinterpreting what your professor said, the problem is that inside your 'catch' block, you do absolutely nothing. You don't print a nice error message, you don't log the error, you don't throw the exception so that somebody else can handle it, you don't show the stack trace...
If the code inside the try block were to throw a UniversalException, your empty catch block would cause that error to be silently ignored, suppressed, or squelched.
The solution is fairly simple -- do something inside the catch block. Errors should never be silently ignored -- at minimum, you should at least print or log something so that you know something went wrong.
Now, currently, it looks like you're actually printing an error message in the constructor of the UniversalException class. I would instead move that into the catch block. An empty catch block feels sort of like a fly walking across your arm -- it just feels wrong, even if you do log the error somewhere else.
The reason why many coders have this sort of instinctual revulsion to empty catch blocks is because it usually indicates a landmine. Whenever I see an empty catch block, it usually means that the previous coder deliberately chose to ignore an error in the code and hope for the best, which is almost never the right thing to do. It's not the kind of thing that makes you optimistic about the state of the codebase.
It's also more useful to print the error message inside the catch block instead of in the constructor since you can also print out the values of local variables in the method and examine the specific exception thrown, which makes it easier to debug the problem.
Your code catches an exception (in this case, an instance of UniversalException) and does not act upon it. This way, since the exception is neither allowed to propagate "upwards" nor is it handled in any way, you are effectively hiding the fact that it ever occurred.
Is the following way to code good practice?
try {
//my code here
} catch (Exception e) {
logger.error("Some error ", e);
throw new MyCustomException("Some error ", e);
}
Moreover, should I..
use only the logger?
throw only the exception?
do both?
I understand that with throw I can catch the exception in another part of the callstack, but maybe additional logging has some hidden benefits and is useful as well.
Normally, I'd argue that you should either log or rethrow. Doing both will just cause every layer to log the exception again and again, which makes the logs hard to read. Even worse, it's hard to figure out how many errors you actually have - was it seven errors, or seven layers of the app which logged the same error?
This means that if you suppress an exception, you log it and say why you didn't think it was worth rethrowing.
On the other hand, if you re-throw the exception, you know it's either going to be caught and suppressed (in which case the catcher logs the exception and why it was suppressed), or it will bubble up out of your app and be caught by the app container, which will catch and log the exception. Every exception shows up once and only once in the logs.
I use both in some cases, logging and throwing the exception. Especially, it's useful in APIs. By throwing the exception, we allow the caller to handle it, and by logging, we can identify the root cause of it ourselves.
And, if the caller is in the same system, then if we add logs in every catch, there will be duplicate logs.
When using the pattern you suggest, you usually end up with error events being reported multiple times in the log. In addition, it's not always simple to connect between them when reading the log.
Personally I prefer logging error events only once, and doing it in the higher call levels. Therefore I almost never log & re-throw. I usually let the exception go up the call stack until it reached a context where it can be handled somehow, and this is where I log.
If the exceptions are wrapped and re-thrown correctly, the context should be perfectly clear from the stack traces of the single log message.
The proper answer would be: "it depends"
You do want in general log the exceptions that you catch, since they correspond to something going wrong. That is why code analysis tools such as sonar will raise warnings when you do not log them.
Consider the following taks: parsing a file. While parsing the file you actually try to parse each line. Sometimes some lines will be malformed, and therefore you don't want to stop parsing the file because of it. In that case, you probably want to just log the wrong line and keep going on the file.
However, imagine that at some point you encounter an I/O exception while reading (for example some other program deleted the file while yours was accessing it).
In this case, you will probably want to log your log the error you encounter, and throw a new exception to stop processing the whole file.
So in short, you have to think about what is the best thing to do. But both practices are not bad.
I think you may need to use the pattern judiciously. As you've written the above, for each exception you're going to log 2 stacktraces and that may fill your logs with excessive information.
With respect to logging vs. throwing, they're two separate concerns. Throwing an exception will interrupt your execution, prevent any further work, perhaps rollback database commits etc. Logging will simply dump info to the log file (or elsewhere). It's of more use for debugging, and often much more difficult to test.
I know that is really old question, but I have an another solution.
Consider this. You can log the problem in catch block and throw a new unchecked exception (of course with passing the previous one inside). In such solution there is no overflow in the logs and exception still bubble up to the highest level.
try {
//my code here
} catch (SomeException e) {
logger.error("Some error occured", e);
throw new MyUncheckedException("Some error ", e);
}
My opinion.
When you need to throw an exception, no matter if it is a Checked or UnChecked(Most RD would not catch this.). It means that you want to do something else by stopping the process.
When you need to log something.
It means you want to track it.
It's quite different things.
You need to check what you really need.
BTW: If your exception was thrown by getting no data from DB.
I suggest you to modify the process to return an empty Data Object(POJO). It is better than throwing an exception.
I asked this question: Printing the stack trace from a newly created thread when an unchecked exception is thrown in Eclipse
And got this in a reply by the user Gray: "By default there is nothing that prints the exceptions thrown by thread-pool threads."
I'd like to know the reason why that is. It seems confusing to me to have a thread-pool thread throw an unchecked exception and stop working without printing any errors by default.
One would have to alter the program, for instance a Thread has a set(Default)UncaughtExceptionHandler.
With AOP one could achieve such a thing.
Because if it printed it, another developer would ask why it cripples the output of the program with unwanted stack traces, and reduces the throughput drastically by writing the stack traces to a synchronized PrintStream. If you want the exceptions to be printed, then print them by yourself. Nothing prevents you from doing it.
The answer you got shows how to do that. You could also wrap all your runnables with another runnable that catches the runtime exceptions, prints them when they are thrown, and rethrow them.
I created a CustomExceptionClass that its messages are from a configuration file and are a friendlier message to the user.
Then I want to log wich exception was thrown because, if something went wrong, I want to know details then I can fix this. So, I have a doubt.
Where to log these exceptions using log4j? Inside the CustomExceptionClass, or I let the method that throws this exception log it?
You should log wherever you are catching the Exception. If you are not catching it anywhere it really depends how you are running your application.
I'm not sure if you have a question about log4j in particular, but that API simply requires something to call log.error(Object,Throwable), passing in the message as the first parameter and the error as the second parameter. (log is of course a Log4J logger reference.)
Regarding the question of where to call log.error, do not call log.error from within your CustomExceptionClass subclass of Throwable. Instead, I'd make the decision as follows:
If you want to log details about exactly what happened, but you don't plan to put those details into your subclass of Exception, then log the details before throwing the error.
Similarly, if you want to log something specific regardless of whether or how the Exception is caught, then obviously you need to do this before throwing the Exception. You have relatively little control over who calls a non-private method.
Otherwise log in the catch block. This allows you to track both what happened and how your application responded as a result. If you just have the first piece of information, then other people will have to read the code in order to understand "so what?"
Finally, it's considered to be good practice to use uncaught exception handlers for all threads. See Thread.UncaughtExceptionHandler for more details. Basically you really want to at least log all Exceptions in long-running applications.
You should log it in the error handling code, not where the error is initially created.
I typically overload ToString and/or GetMessage in custom exceptions and simply log them per normal.