Jump to content

[Important] OSBot golden scripting rule: Exception handling in OSBot


Maxi

Recommended Posts

  • Developer

Hi everyone,

 

It has come to my attention that it's not clear for everyone how to handle exception in OSBot scripts. Various pieces of code in the OSBot client throw InterruptedExceptions. Everywhere where you have a try-catch block you have to make sure you add specific clause to catch InterruptedExceptions in which you then have to throw, in front of any other catch clause. If you're only adding try-catch blocks to catch the InterruptedException, then make sure you are just adding the throws statement to the method signature and remove your try-catch block entirely.

 

The reason this is important is because the client uses interrupted exceptions to switch behaviour, for example when the bot will switch from the script to a random executor, or when you pause your script.

 

Here is an example:

 
    /**
     * Gets called recursively on the bot's main thread.
     *
     * @return The timeout to sleep after running one loop cycle.
     */
    @Override
    public int onLoop() throws InterruptedException {
        try {
            scan();
            switch (state) {
                case IDLE:
                    return 100 + gRandom(100, 50);
                case STEAL:
                    return steal();
                case TO_BANK:
                    return toBank();
                case BANK:
                    return bank();
                case TO_STALL:
                    return toStall();
            }
            return 20 + gRandom(100, 100);  
        } catch (InterruptedException e) { // <---- VERY IMPORTANT ----> \\
            throw e;                       // <---- VERY IMPORTANT ----> \\
        } catch (Exception e) {
            e.printStackTrace();
            return 100 + gRandom(200, 100);
        }
    }
Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...
  • 3 weeks later...

I don't really understand why you would throw an exception that is already being thrown.  You should probably add code there to handle the exception.... rather than just throw it again.  

 

People who didn't know this yet probably script in Notepad.  smile.png

People who can use just a text editor to program are probably above you, just sayin'.

Edited by pwnd
Link to comment
Share on other sites

  • 2 weeks later...

I don't really understand why you would throw an exception that is already being thrown.  You should probably add code there to handle the exception.... rather than just throw it again.  

 

People who didn't know this yet probably script in Notepad.  smile.png

People who can use just a text editor to program are probably above you, just sayin'.

 

Haha, I remember when I was into RSPS coding I used to program stuff with a pencil in paper when I was bored in classes. Writing scripts is a bit more complicated for me though. :P

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...