Shop OBEX P1 Docs P2 Docs Learn Events
Try / Catch — Parallax Forums

Try / Catch

BobValBobVal Posts: 48
edited 2005-11-29 21:15 in General Discussion
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

Comments

  • BobValBobVal Posts: 48
    edited 2005-11-27 00:47
    Well after almost 3 days I have had no unexplained crashes, out of memory conditions, stack errors, etc.

    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-11-27 01:09
    Maybe try/catch reserves more stack space (and additional code for debugging).

    regards peter
  • 4ish4ish Posts: 24
    edited 2005-11-27 19:39
    I was actually gonna post a question asking how I could stop these unknown exceptions... but I'm going to try it your way and see what happens...

    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...
  • 4ish4ish Posts: 24
    edited 2005-11-27 19:46
    [noparse]:([/noparse] no luck

    I just got this

    "Unknown bytecode in the JEM file (ldc2_w)" Error IDE -0024

    any help would be greatly appreciated...
  • 4ish4ish Posts: 24
    edited 2005-11-27 19:50
    and now another one "IDE - 0042 This is an internal error. Report this to technical support."
  • BobValBobVal Posts: 48
    edited 2005-11-28 03:18
    Interesting. After my success with having gotten passed the stack corrupted, out of memory, etc because of putting the "try / catch" I have not put them around all MY code. Just incase.

    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
  • 4ish4ish Posts: 24
    edited 2005-11-28 15:14
    I'm going to post my code, it would be great if you/someone takes a look at it and tells me what I should change...

    thanks a lot
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-11-28 16:44
    I haven't read all your code yet but the following is wrong:

    ·//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

    ·
  • 4ish4ish Posts: 24
    edited 2005-11-29 00:23
    thanks peter... I understand what you mean... I've since removed creation of new objects in loops as much as I can.. but there are still instances that I need to create the objects...

    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 ...
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-11-29 01:13
    I always commit myself to a few rules:

    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
  • 4ish4ish Posts: 24
    edited 2005-11-29 20:08
    I've changed most of all to StringBuffers which I append and clear in the loop... and I've changed the XOR function to work with StringBuffers

    suppose you want to use the sendString method in a UART ... does toString() have the same affect ?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-11-29 21:15
    Here is the javadoc for toString in StringBuffer.java
    · /**
    ·· * 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
    ·
Sign In or Register to comment.