Custom Runtime exception not printing the stack trace - java

Sorry for this very basic question, but I am stuck with this and m not able to find a solution.
I have a Core Java application (Java version is 1.6). From my application I am calling a method in a jar, which is throwing a custom runtime exception.
I am not catching this exception, but still JVM is not printing the stack trace.
Does JVM by default will not print the stack trace, when a runtime exception is thrown and not caught?
Or am I missing something which I can check?
Thanks in advance.
-Sandeep

AFAIK, JRE should print a stacktrace of an uncaught exception. But you can always do it yourself:
try {
someObject.someMethod();
} catch (Throwable t) {
t.printStackTrace();
throw t;
}

Related

Exception Handling in JS using Graal

I work on a Java application that makes fairly heavy use of Javascript to form the business logic/glue. It runs using Graal. This all works fine, but we struggle with effective error handling.
This is essentially how the JS is executed:
try {
Context context = Context.newBuilder("js").allowAllAccess(true).build()
Source s = Source.newBuilder("js", src, "script").build();
context.eval(s);
} catch (Exception e) {
LOGGER.error("Exception occurred in JavaScript:...", e);
}
So when errors happen we log them somewhere so we can do some postmortem, etc. It's possible to get the JS stack trace in these logs out of the PolyglotException that Graal throws, which is great. However, things are more complicated when some JS code has called back into Java-land, and a Java exception has been thrown:
var x = callJavaFunction("invalid parameter"); // Throws a NoSuchElementException, for example
The PolyglotException has an asHostException() method that returns the original Java-land exception, and my code that executes the JS files is smart enough to understand this and produce a useful error log. The problem arises when the JS code has tried to catch this itself, for whatever reason:
try {
var x = callJavaFunction("invalid parameter"); // NoSuchElementException
} catch (e) {
doSomeCleanup();
throw e;
}
Now we have lost the original Exception, and even worse, the JS-stack trace now just shows us the catch block, instead of where the cause was. isHostException() returns false, because this is just a JS error now. I cannot find a way to get at the original cause, which makes diagnosing errors quite difficult, especially when they have come out of a production system. The original Java exception message ends up in the JS-error object, which is helpful, but we don't have the stack trace, which is not.
What approaches can I take to try and address this?
One thought I had: Can I hook into the GraalVM and get a callback whenever a host-exception is thrown? At least that way I could have a log saying "the following Java Exceptions were thrown during execution" which I could attach to the error report. So far I've not been able to find a way to achieve this.

Console not showing Runtime Exception

I found a really weird bug within my Java Code
Even when i force a RuntimeException in my program it is not recognized by the JVM.
Here a demo of what I have written
private static void someMethod(){
//Some Code
if(true)
throw new RuntimeException();
// More Code
}
I added the if(true) to prevent the unreachable code message, just for testing.
But I think that the real problem is that there is some unhandled Exception in my code, which I cant really log, because the printStackTrace() is missing, or else i should get a console log.
Also I get the plain text: Exception while removing reference.
But its no System.err message, it just look like System.out
Are there any other methods of logging exception, excpect the default console, and what could cause a exception to be unhandled?
NOTE: I use following external libraries: JNativeHook, JLayer, Apache Commons IO
Full GitHub repo
The Exception should occur in CsgoSounds.java at line 944
OS: Windows 10, jre version: 1.8.0_60
There are checked Exceptions and unchecked ones (everything that extends runtime exception).
The compiler force you to deal with checked exceptions (with a try catch or a throws declaration). You are not forced to deal with Unchecked exceptions. But you can, just add a try catch around your code, then you can call printstacktrace on it.
JNativeHook was blocking all console outputs. I had to enable the function first.
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.WARNING);
did the job.

Why doesn't the stack trace get printed?

While trying to print the stack trace in my browser window, I fail. Following is the sample of the exception block.
try {
// Add Code Here
} catch(Exception exc) { exc.printStackTrace();}
I am using google app engine as my server. What could be the reason the stack trace is not printing ?
Because there is no Exception to catch:
try {
// Add Code Here
}
You will only print a stack trace an Exception is thrown within the try block like this:
try {
throw new RuntimeException();
}
The method in question is implemented like this:
public void printStackTrace() {
printStackTrace(System.err);
}
If your container/IDE/framework redirects the stream then you will not observe it at the expected location, see also System.setErr(...);. And of course, I expect you to have checked that your code really throws an Exception (if an Error then it will not be caught by your clause).
You will need to change the default error output stream. Since you are most probably coding a servlet , you could change to :
exc.printStackTrace(response.getWriter());
This will print the exception trace into the browser window.

Source not found for Null Pointer Exception

I've come across some really strange behaviour in my Java code. There is a exception shown on my Eclipse log console saying Exception:java.lang.NullPointerException with no reference to the code where it occurred.
On debugging I found out a line where this occurred and so put it in try-catch hoping I catch it. However it didn't return in catch block.
The strange part being even though there's exception thrown at the line immediately after it executes and the execution continues normally.
Can some one please tell me the probable cause?
I could have attached the source code but I have checked the parameters and all seem fine.
My main reason for this post is to learn about such behavior if any of you coders ever came across.
Probably a problem with Eclipse. I have seen that behaviour before, and restarting Eclipse solved the problem.
Please check whether your builder is activated and the changed source code is build automatically. Otherwise your code changes will never get it into your runtime application.
I am pretty sure, that the executed source code is different to the source code shown in your editor.
If you see the exception's message but no stack trace, that is caused by code that looks like this:
try
{
// something which causes the exception
}
catch (final Exception err)
{
System.out.println(err);
}
The problem with this code is that it only prints the result of the exception's .toString() method. For most exceptions this is just the exception class and the message. This code omits the stack trace, thus making it much harder to debug the problem.
If the exception is to be caught, then change the code to look like this for the stack trace to be included in the output:
try
{
// something which causes the exception
}
catch (final Exception err)
{
err.printStackTrace();
}

SQLiteException not being caught

I'm trying to catch an "android.database.sqlite.SQLiteException: error code 5: database is locked" exception with:
try {
db.insert("mytable", null, myvalues);
} catch(SQLiteException e) {
Log.d("My App", "caught");
...
}
For some reason, I still get the error, and "caught" doesn't show up in LogCat. I tried catching a general "exception", but that still doesn't work. What's wrong?
UPDATE
I found the issue, and it is really weird: for some reason changing db.insert() to db.insertOrThrow() as goto10 stated magically fixed everything. The error was comming from that line, but maybe it wasn't throwing an exception and only crashing or something?
I don't believe that .insert will throw an exception. You're probably just seeing a log message that's being written by it when it catches the exception internally. If you want it to throw an exception when an insert fails, use .insertOrThrow instead.
Try this
try {
} catch( SQLiteException e) {
Log.e("My App",e.toString(), e);
}
If that catch block is failing to catch the exception then either it isn't being thrown inside that particular try block, or you've got the wrong exception. The fact that it still doesn't work if you catch Exception says that it is the former problem.
So you need to find out where the exception is really being throw. I'd try changing the logging configurations to get LogCat to log full exception stacktrace. That should tell you where it was thrown. If you can't do that, then you'll need to find this using a debugger ... or by trawling through your applications source code to find where the exception is being logged.
(The other possibility is that the catch block is catching the exception, but you've got your logging configs set up to discard "debug" log events.)
The exception is probably thrown when you open the database, not when you insert a new row.

Categories