Author Topic: Java, jump back in code possible?  (Read 2602 times)

0 Members and 1 Guest are viewing this topic.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Java, jump back in code possible?
« 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.



Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Java, jump back in code possible?
« Reply #1 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
« Last Edit: October 16, 2014, 10:20:27 pm by HTH »
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java, jump back in code possible?
« Reply #2 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
« Last Edit: October 17, 2014, 09:09:56 am by Deque »

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Java, jump back in code possible?
« Reply #3 on: October 17, 2014, 09:14:05 am »
I love GOTO and global variables!
Who the fuck needs returns anyway.
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java, jump back in code possible?
« Reply #4 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.


Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Java, jump back in code possible?
« Reply #5 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)"
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Java, jump back in code possible?
« Reply #6 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.
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Java, jump back in code possible?
« Reply #7 on: October 17, 2014, 02:15:57 pm »
Acknowledgement of Sarcasm
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Java, jump back in code possible?
« Reply #8 on: October 17, 2014, 04:29:51 pm »
thank you all, I managed it (some combination of while and exceptions...)

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Java, jump back in code possible?
« Reply #9 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.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Java, jump back in code possible?
« Reply #10 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?
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Java, jump back in code possible?
« Reply #11 on: October 18, 2014, 10:06:44 pm »
Yup, but rolling with the breeze. When did EZ become so stall.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline Architect

  • Sir
  • ***
  • Posts: 428
  • Cookies: 56
  • STFU
    • View Profile
    • Rootd IRC
Re: Java, jump back in code possible?
« Reply #12 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.