what happens when you pass android systemclock.sleep(long) a negative parameter - java

Expectedly, this SystemClock.sleep(paramMaybeNegative) is being used in a multi-threaded context, if that is relevant. The negative would come from an uninitialized object property in some yet unforseen circumstance.
Here is the documentation regarding what I am asking about:
developer.android.com:SystemClock.sleep(long). It doesn't mention an exception or forever or something else that might be expected.
Putting this question here to remember to answer it later and handle the consequences in some code I am writing, as much as in hopes someone knows what to expect already and wants to type about it before I get to (super-informative and helpfully interesting factoids about SystemClocks also welcome:).

It calls Thread.sleep(millis) which throws an IllegalArgumentException.
You can find out by reading the src code, or by trying out.

From reading and guessing at source code running in emulator I would expect it to not throw error and act like a SystemClock.sleep(0); it throws to logcat and crashes app:
java.lang.IllegalArgumentException: timeout arguments out of range
at java.lang.VMThread.sleep(Native Method)
...
...
...
I am as likely as not looking at wrong source file.

Related

Catching java exception in Jython

I have an exception I'm trying to catch whenever my VNC connection is killed (whenever I restart my VM) and I'm trying to ignore it to continue my Jython script. I'm not sure how to write a try catch statement to catch the exception since it's originating from Java. This is for a sikuliX script.
Exception in thread "Thread-7" com.tigervnc.rdr.EndOfStream: EndOfStream
at com.tigervnc.rdr.FdInStream.readWithTimeoutOrCallback(FdInStream.java:192)
at com.tigervnc.rdr.FdInStream.overrun(FdInStream.java:142)
at com.tigervnc.rdr.InStream.check(InStream.java:37)
at com.tigervnc.rdr.InStream.check(InStream.java:44)
at com.tigervnc.rdr.InStream.check(InStream.java:45)
at com.tigervnc.rdr.InStream.readS8(InStream.java:55)
at com.tigervnc.rdr.InStream.readU8(InStream.java:64)
at com.tigervnc.rfb.CMsgReaderV3.readMsg(CMsgReaderV3.java:50)
at com.tigervnc.rfb.CConnection.processMsg(CConnection.java:67)
at org.sikuli.vnc.VNCClient.processMessages(VNCClient.java:246)
at org.sikuli.vnc.VNCScreen$1.run(VNCScreen.java:82)
at java.lang.Thread.run(Unknown Source)
#abarnert You really saved my day. I have encountered the same problem as OP posted. The jython code is as following:
################################################################
# CONFIG DEFINITIONS
################################################################
#GOOD_IMAGE_URL = "http://192.168.1.15/11111111.png"
BAD_IMAGE_URL = "http://192.168.1.15/111111112222.png"
def exceptionDemo(urlPath):
try:
print "exceptionDemo is starting"
myImg = imageio.ImageIO.read(URL(urlPath))
if myImg is None:
print "Img is None"
else:
print "Img is OK"
return myImg
#except Exception, err:
except java.lang.Exception as err:
print "Exception is here"
print err
finally:
print "Finally is here"
the purpose of this code is to download the image file specified by urlPath. when the ulrPath is bad(in this case, the 111111112222.png does not exist.), I would like to catch the exception in order to handle this issue properly.
If I simply use the normal "except Exception, err" to catch the exception, the program will just abort without any information. The print statements in except and finally section are supposed to execute, however, that didn't happen. What I get is nothing after seeing "exceptionDemo is starting".
I spent days on dealing with this problem, searching on google again and again, leaving no stone unturned. After days of investigation, I get the idea that there are two sorts of exceptions when you programming in jython, one is python exception, the other is java exception. The reason for this problem is that either jython or SikuliX in this case, has suppressed the java throwables. I am not very familiar with jython, so it's just my guess and it has not been verified(I am more familiar with native Python and Java).
At first, I doubt it's ImageIO - the native java package's problem. I wrote a simple java demo to test ImageIO by giving the bad image url, it worked great, a nice "javax.imageio.IIOException: Can't get input stream from URL" had been thrown out. Then, I know that my guess is right. The java exception somehow did not well handled in jython or SikuliX. The point here is that even if this is the root cause, I still need a simple and elegent way to deal with this problem. I tried some other workarounds, but nothing is as good as I've expected. The essential part of this problem is not about downloading the image, it's about catching the exception.
It was not until I simply replace "except Exception, err“ with "except java.lang.Exception as err" that the code is working like a charm. I got all the printouts that I expected.
Your posts really saved my day. Good job and many thanks.
The following references are very helpful in my investigation:
javax.ImageIO methods fail silently
trap exceptions comprehensively in Jython
https://www.jython.org/jythonbook/en/1.0/ExceptionHandlingDebug.html
Finally, If there were any tips I would like to offer to people who find this post, it should be "STAY AWAY FROM THIS KIND OF HALF-BAKED SHODDY HYBRID LANGUAGE", You will get neither the expediency of Python, nor the robustness of Java, the only thing that you eventually will get is a big waste of your time.
=================================================================
Update on 2019-08-02:
As a follow-up of this post, I offer you a "buy one get more free" deal. It contains a heck of surprise brought you by Jython and Sikuli X.
1, DO NOT USE ANYTHING DEPENDS ON Netty in Jython. This includes httplib and urllib2, etc. It will fail if you try to GET/POST something in a loop, use Requests or java.net instead.
for details, please refer to this post: Why does this Jython loop fail after a single run?
2, DO NOT USE type() function in Sikuli X. This function is normally used in python to determine the type of an object, however, Sikuli X use it to mimic the keyboard input function. If you insist, you should import builtin first, then call it like this:
# -*- encoding: utf-8 -*-
import __builtin__
if __name__ == '__main__':
a=10
print __builtin__.type(a)
for details, please refer to these:
How to check variable type using “type” function in Sikuli
https://answers.launchpad.net/sikuli/+question/239574
These are some pitfalls that I have experienced in jython and Sikuli X, if you have to work with these two things, good luck and wish you have fun.
As explained in Exception Handling and Debugging, you handle Java exceptions in Jython the same way you handle Python exceptions:
As stated previously, it is a common practice in Jython to handle Java exceptions. Oftentimes we have a Java class that throws exceptions, and these can be handled or displayed in Jython just the same way as handling Python exceptions.
A Java exception is, as far as your code is concerned, just an instance of some Exception subclass.
Of course that subclass happens to live in, e.g., java.lang instead of builtins or another stdlib module, and it will (almost?) always have java.lang.Exception as an intermediate ancestor before getting all the way back to Exception, but generally, you don't care about that.
So, wherever you're calling the Java code that's throwing this exception… just put a try: / except Exception as e: or except java.lang.Exception as e: or more specific type, the same way you would when calling Python code.

What is the "reached end of file while parsing" error, and how can I avoid this error in the future?

My question is very simple. I am just wondering what the reached end of file while parsing error actually means / is. Why this error actually occurs. In addition, how I could avoid it. Before you say that this is a duplication of question it really isn't because most questions I looked at are just examples with code. That is not what I am looking for I am just looking for a clear cut answer of why this error happens what it is / means, and how to avoid it that is it.
NOTE: I don't understand "reached the end of the file". I know what parsing is, but I don't know what it means by reached the end of file: while parsing. I also just want to now how to avoid this error, and why it occurs not just to understand it.
You could avoid this error by fixing the syntax in the code causing the parser to expect additional closing syntax indicators (like a right curly-brace). Also check the responses to the duplicate questions because they clearly explain what are generally the syntactical causes of same error.

Running a java application (JAR file) on commandline, have errors that I don't understand

Not sure what these errors mean as I have searched some of them such as "Exception NO enum constant" but only "no enum const class" comes up answered on google. Can anyone examine the error at the bottom of command prompt and tell me what the error might be from? Image of the cmd error attached below.
As requested I have put the source code for org.evosuite.Properties.Algorithm which showed that there was an enum error. The entire class is too big but innside algorithm I have alreayd put "CELLULARGA" and the rest of the types of algorithms run so I'm unsure why there is that exception error when I run with cellularGA
// ---------------------------------------------------------------
// Search algorithm
public enum Algorithm {
STANDARDGA, MONOTONICGA, ONEPLUSONEEA, STEADYSTATEGA, RANDOM, NSGAII, MOSA, CELLULARGA
}
It's an IllegalArgumentException, which means you have passed in wrong arguments (probably due to type mismatch) to some method in your code. And it says it's on line 238 so you should start there.
You should practice catching exceptions with code that you can understand and which gives you a clearer picture of where the problem is instead of just printing a stacktrace you clearly can't interpret.
Except it's a runtime exception, in which case you might just have to study them.
Also, you would get more help here if you showed us some source code...

Is it a good practice to log line number in error code to the user?

I have been tasked with logging the line number as part of the error code shown to the user. Currently I am using:
StackTraceElement[] stackTraceElement = e.getStackTrace();
lineNumber = stackTraceElement[0].getLineNumber();
I know that the above approach may fail depending on the JVM version.
Also, I have seen the PatternLayout where it is mentioned that "Generating caller location information is extremely slow. Its use should be avoided unless execution speed is not an issue.".
Since this message will be presented to the user, should I still log the line number as part of the error code? I am trying to understand the pros and cons of this approach. Also, does the log4j warning apply only to its own implementation or rather is it a warning against location information generally?
Well, generally speaking your program should report two kind of errors:
The errors that are for the user (when the user is not doing what is expected from him), which should actually be better called "feedback" to help him feed your program with the right data (that's good UX practice).
The errors that are generated because of a bug, which are actually not targeted at the user, but at you the developer, sadly through the user. Then yes, it might be a good idea to log line numbers (or give your errors unique names/identifiers so that you can trace easily where it's been sent from). But a better idea is to then use a framework to report such issues directly to you through Internet (good practice being to ask for permission first).
What you should show to the user is what went wrong and what he can do about it, if anything. The line number information needs to be available, e.g. via a 'More details' button, in case he needs to raise a support ticket, but you don't want to frighten him or confuse him with it up front. Just look at how many stack traces get misread or indeed ignored completely here, and this community is supposed to be computer programmers.
Generally its better if your program doesn't give errors, and can receive all input, and give you tips on how to use the application. This will give users a much better experience if you want them to buy your product, etc.. If your program does give errors, it will not be helpful for the user to know the line number. You however will want to know the line number, so you should make it display a message of some sort that tells the user to email you the stack trace when the error occurs. Or you could have it report the error message automatically and email it to you.
Sorry, I'm a bit late I was unclear what the question was asking, so I posted this as a comment, but clearly it is an acceptable answer.

Strange androidNDK - java, only the first line of code is running

i have a method something like this in my code (java).
public void DoSomeStuff() {
Log.i(TAG,"1");
Log.i(TAG,"2");
Log.i(TAG,"3");
}
This is called from C++ through JNI. I'm 100% sure the JNI works and has nothing to do with my problem.The problem is: Only the first line of the code runs (the output is "1"), and the rest gets ignored. It doesn't matter what i write in the first line... only that gets executed.There aren't any error messages, freezes, or any relevant information to help in debugging.I have also tried to clear the bin/gen folders but no success.The worst part is that, sometimes it works... and sometimes it doesn't.
Thanks
you could post the code for i method, maybe there is something within its body that is causing this behaviour?
Have you tried calling different methods? Not neccessarly from Log?
Good luck.

Categories

Resources