EvilZone

Programming and Scripting => Java => Topic started by: DreX on October 16, 2014, 09:49:58 PM

Title: Java, jump back in code possible?
Post by: DreX on October 16, 2014, 09:49:58 PM
Is there a way I can jump back in code in Java.
Example:
   The programm senses an error, it calls a method. The method preforms a "hard reset (a part of programm that puts back everything they way it was-for the question it isn't important what this does) and then starts the main method from start again (without finishing the main method).

I can't (don't know how) to use break/return because they only exit the inner loop...and I already have an inner loop (the loop that checks for errors..)...so return/break would only escape the error checking loop and would not return me to the start.


Title: Re: Java, jump back in code possible?
Post by: HTH on October 16, 2014, 10:15:26 PM
Ironically, and IIRC btw, you can use a break. Java doesnt let young programmers use Goto becase gotos are evil and bad programming, but if you need to break out of a larger loop based on something inside of a smaller loop... you can do this:
Code: [Select]
outer:
for (int i = 0; i < I_MAX; i++) {
for (int j = 0; i < J_MAX; j++){
// bla bla bla error checking or something
break outer; //Will break out of the loop designated by outer.
}
}
Now i'm not a java coder at all; hate the fucking stuff. But if i was a java coder and you wanted exactly what you said in your post I'd say use something like this;

Code: [Select]
Boolean isGood = true;
outer:
for (int i = 0; i < I_MAX; i++) {
for (int j = 0; i < J_MAX; j++){
// bla bla bla error checking or something
isGood = false;
break outer; //Will break out of the loop designated by outer.
}
}
if (isGood) {
doSomething();
} else {
doSomethingElse();
}

BUt like I said Im no Java coder and Im sure someone else will chime in
Title: Re: Java, jump back in code possible?
Post by: Deque on October 17, 2014, 09:00:11 AM
The problem you describe is best handled with exceptions.
Example:

Code: (Java) [Select]
public static void main(String... args) {
    //you told me, you want to jump back here, so we place a check if the call was successfull
    boolean success = false;
    while(!success) {
       try {
           //you call your method, if an exception occurs, you will jump to catch
           doSomething();
           // if no exception occured, this will be executed
           success = true;
       } catch (IllegalStateException e) {
           // you may decide to print the message here
           System.err.println(e.getMessage());
       }
   }
}

public static void doSomething() throws IllegalStateException {
     //you call your method, but you don't care about handling the exceptional case here
    int i = problematicMethod() + 1;
    // is only executed, if no exception occurs
    System.out.println(i);
}

private static int problematicMethod() throws IllegalStateException {
    //something goes wrong
    if(somethingIsWrong()) {
        //the rest of the code in this method will not be executed
        throw new IllegalStateException("error message");
    }
    int i = 1;
    return i;
}

While that might seem more complicated than jumping (which is only partly possible in Java), it is actually less error prone with large projects. You have a declaration for the exceptional exit. Every caller will know that an exception might happen and can decide to handle it now or throw it further.

----------------------------------------------

Ironically, and IIRC btw, you can use a break. Java doesnt let young programmers use Goto becase gotos are evil and bad programming,

Java does not let old fags do that either.  :P
Title: Re: Java, jump back in code possible?
Post by: proxx on October 17, 2014, 09:14:05 AM
I love GOTO and global variables!
Who the fuck needs returns anyway.
Title: Re: Java, jump back in code possible?
Post by: Deque on October 17, 2014, 09:16:46 AM
I love GOTO and global variables!
Who the fuck needs returns anyway.

From now on I only want to see assembly code from you and nothing else.

Title: Re: Java, jump back in code possible?
Post by: HTH on October 17, 2014, 11:17:23 AM
The problem you describe is best handled with exceptions.
Example:

Code: (Java) [Select]
public static void main(String... args) {
    //you told me, you want to jump back here, so we place a check if the call was successfull
    boolean success = false;
    while(!success) {
       try {
           //you call your method, if an exception occurs, you will jump to catch
           doSomething();
           // if no exception occured, this will be executed
           success = true;
       } catch (IllegalStateException e) {
           // you may decide to print the message here
           System.err.println(e.getMessage());
       }
   }
}

public static void doSomething() throws IllegalStateException {
     //you call your method, but you don't care about handling the exceptional case here
    int i = problematicMethod() + 1;
    // is only executed, if no exception occurs
    System.out.println(i);
}

private static int problematicMethod() throws IllegalStateException {
    //something goes wrong
    if(somethingIsWrong()) {
        //the rest of the code in this method will not be executed
        throw new IllegalStateException("error message");
    }
    int i = 1;
    return i;
}

While that might seem more complicated than jumping (which is only partly possible in Java), it is actually less error prone with large projects. You have a declaration for the exceptional exit. Every caller will know that an exception might happen and can decide to handle it now or throw it further.

----------------------------------------------

Java does not let old fags do that either.  :P


I knew someone would chime in with the technically correct answer :D I didn't know (or at least remember) exactly how to deal with exceptions in java. And I meant it more of "They didnt implement it so young programmers who dont know any better couldnt release code filled with GOTOs... because fuck GOTOs. And Global Variables. (Proxx)"
Title: Re: Java, jump back in code possible?
Post by: proxx on October 17, 2014, 12:35:11 PM

I knew someone would chime in with the technically correct answer :D I didn't know (or at least remember) exactly how to deal with exceptions in java. And I meant it more of "They didnt implement it so young programmers who dont know any better couldnt release code filled with GOTOs... because fuck GOTOs. And Global Variables. (Proxx)"
Sarcasm.
Title: Re: Java, jump back in code possible?
Post by: HTH on October 17, 2014, 02:15:57 PM
Acknowledgement of Sarcasm
Title: Re: Java, jump back in code possible?
Post by: DreX on October 17, 2014, 04:29:51 PM
thank you all, I managed it (some combination of while and exceptions...)
Title: Re: Java, jump back in code possible?
Post by: kenjoe41 on October 18, 2014, 07:25:38 PM
I love GOTO and global variables!
Who the fuck needs returns anyway.
Fkcu ball, come to C++ and sure as hell you will be wiping your nose with a door mat if you try GOTO stuff in a sensible project. I wonder if mozilla cleaned up its code cos it had alot of those.
Title: Re: Java, jump back in code possible?
Post by: proxx on October 18, 2014, 09:16:50 PM
Fkcu ball, come to C++ and sure as hell you will be wiping your nose with a door mat if you try GOTO stuff in a sensible project. I wonder if mozilla cleaned up its code cos it had alot of those.
You got the part where I was joking too?
Title: Re: Java, jump back in code possible?
Post by: kenjoe41 on October 18, 2014, 10:06:44 PM
Yup, but rolling with the breeze. When did EZ become so stall.
Title: Re: Java, jump back in code possible?
Post by: Architect on October 20, 2014, 05:33:38 PM
Yup, but rolling with the breeze. When did EZ become so stall.
The real hackers are here still, just fapping to more useful things.