Try / Catch
BobVal
Posts: 48
Hello:
I had written a lot of code and for some reason making a certain change would start giving me stack errors, out of memory errors, etc.
I could not figure what was wrong so I decided to "try and catch" these errors.
······ public static void main() {
· ············· try· {
··· ················· while(true)· {
····· ··············· }
······· ······· catch(Exception e)· {
········· ··········· System.out.println(e.getMessage());
··········· ··· }
······ }
Since putting this "try and catch" around all my code (hoping to catch the error in the debugger so I could debug it) I have not had any other exceptions and everything seems to run fine.
Has anyone had this problem?
Does anyone know why putting the "try / catch" around all my code made the weird errors stop?
BobVal
I had written a lot of code and for some reason making a certain change would start giving me stack errors, out of memory errors, etc.
I could not figure what was wrong so I decided to "try and catch" these errors.
······ public static void main() {
· ············· try· {
··· ················· while(true)· {
····· ··············· }
······· ······· catch(Exception e)· {
········· ··········· System.out.println(e.getMessage());
··········· ··· }
······ }
Since putting this "try and catch" around all my code (hoping to catch the error in the debugger so I could debug it) I have not had any other exceptions and everything seems to run fine.
Has anyone had this problem?
Does anyone know why putting the "try / catch" around all my code made the weird errors stop?
BobVal
Comments
I have one chip running no-stop for 3 days and another I have been updating as I make code changes.
Now I do not completely understand what the try / catch around my code is causing the compiler or linker to do to stop these errors, but it must be doing something.
If I take out the try / catch then evey so often my program will start getting these stack erros, etc when using the IDE.
BobVal
regards peter
problem is once my program had these exceptions, a reset wouldnt run it again... I'd have to reprogram... and sometimes I get an "unknown error occured in the JVM" and it says its an internal error and I should report it to parallax...
now I dont need the program to run for days.... 30 mins should be fine... but since I am going to be demonstrating this infront of the my department I really cannot afford any exceptions, specially those that require to reprogram.
others, please let me know about your experiences with this...
I just got this
"Unknown bytecode in the JEM file (ldc2_w)" Error IDE -0024
any help would be greatly appreciated...
But before doing this I was once in a while getting some of the errros you mention above. I kept getting the "Unknow bytecode" and had no idead why and what caused it. I also got the IDE - 0042 a few times and again do not know what cause them or how they went away.
But I would delete routines (I had lots of routines with different overloads - as an old c++ programmer I am use to have 32 ways to call the same function) and I had written most of my code before I actually got the javelin. Anyway having all these routine I thought was causing a problem (in one routine my ezLCD class - I had too many routines or more then was allowed). So now I have the bare basics. Only routines I am using and the try / catch around all my main module routines.
I have know idea why any of this would allow me to work for the last 3 days with NONE of the above problems I was having but it sure has made working fun.
Tomorrow (late Monday evening) I will start removing some of the try / catch's I have to see what effect it has.
BobVal
thanks a lot
·//XOR function for decoding the signal received from the rf receiver
· public static char[noparse]/noparse xor(char[noparse]/noparse password, char[noparse]/noparse key) {
··········· char[noparse]/noparse result = new char[noparse][[/noparse]password.length];
··········· for (int i=0; i<password.length; i++) {
··············· result[noparse][[/noparse]i] = (char)(password[noparse][[/noparse]i] ^ key[noparse][[/noparse]i]);
··········· }
··········· return result;
· }
You create a new char array each time you call xor.·Now this should not give unhandled
exceptions, but it will lead to outOfMemory error because there is no garbage collection.
Or do you keep a list of all created arrays so you can reuse them? I assume you use result
only once· to check or create an encrypted password.
Just define globally
static char[noparse]/noparse result = new char[noparse][[/noparse]maxpasswordLength];
and define xor as
·//XOR function for decoding the signal received from the rf receiver
· public static char[noparse]/noparse xor(char[noparse]/noparse password, char[noparse]/noparse key) {
··········· for (int i=0; i<password.length; i++) {
··············· result[noparse][[/noparse]i] = (char)(password[noparse][[/noparse]i] ^ key[noparse][[/noparse]i]);
··········· }
··········· while (i < maxpasswordLength) result[noparse][[/noparse]i++]=0; //clear remaining of result
··········· return result;
· }
I will read your code further.
regards peter
·
for example
char[noparse]/noparse stored = bufferBattW.toNewString().toCharArray();
wont work for some reason if I use just toString() ...
is this still bad practice? is there anyway around this? or perhaps a way to programmatically reset the uProcessor?
also I've been trying to figure out why I get some of the other errors... I think the "Unknown bytecode" may be related to my XOR function...
the transmitter basically sends a name followed by a 4 digit integer that is incremented by 1 after each send... the receiver however sometimes receives garbage characters or suddenly skips a hundred units (e.g. from 1000, 1001, 1002, to 1132, 1133... and so on)... this isnt such a big problem for me but the garbage characters sometimes manage to break the XOR function to throw that error...
what can I do to decrease this interference that is causing the garbage to show up ?
again thanks a lot ...
Never use the new keyword inside a subroutine !!!
If you do need an temporary array just define a global array and reuse that.
Or use a heap like my heap class:
http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/memory/
That way you don't have to worry about garbage collection.
I always·use (null terminated) char[noparse]/noparse for adaptable strings. I only use Strings
for constants. These are easily converted to char[noparse]/noparse using toCharArray().
(and I only use that because char[noparse]/noparse text = "some text"; is not allowed.)
My format class is build using char[noparse]/noparse. Don't use constructs like "text1"+"text2"
as this creates a temporary string that is used only once.
Many functions in the String class create temporary new strings which may
lead to memory leaks also. Always check what a String method does.
Try to keep your classes small and logical. Create a basic class and for some
rare used methods extend that basic class.
regards peter
suppose you want to use the sendString method in a UART ... does toString() have the same affect ?
· /**
·· * Convert the StringBuffer to a string. A single buffer is used to convert
·· * to a String. Each time this method is used the previously returned
·· * String will be overwritten. If you need to keep the String then use
·· * the toNewString method.
·· *
·· * @return a String representation of the StringBuffer.
·· */
StringBuffer uses one buffer so this is ok if you need the string only once.
You find the classes String and StringBuffer in <ide path>\lib\java\lang
Its very interesting to look at those classes. You'll learn so much more
than from reading the javadocs only.
regards peter
·