Results 1 to 9 of 9

Thread: Try, Catch and Throw Exception Handling

  1. #1

    Default Try, Catch and Throw Exception Handling

    I have no idea if the try-catch exception handling was ever posted before in some of the old-dead mohaa forums, but
    The only mod/script I ever seen using try-catch is the Date and Time API made by Sor.
    That's where I got the info from, so credits to him for the "discovery".

    Here are some conclusions that I came up with:
    - The try-catch exception handling in MOHAA doesn't catch all the exceptions, so you can't avoid the server from crashing with this.
    - You have to manually "throw" each exception you want to "catch"
    - Every exception has to have his own label inside the catch{} block
    - You can only have one catch{} statements after each try{}

    Here's a fictional example code so you can get an idea on how it works:

    main:
    try {
    local.name = "UnnamedSoldier";
    local.arg = NIL;
    local.string = NIL;

    local.result = waitthread getInfo local.name local.arg local.string;
    } catch {
    InvalidNameException:
    conprintf("ERROR: Invalid Name\n");
    end;
    InvalidArgumentException:
    conprintf("ERROR: Invalid Argument\n");
    end;
    InvalidStringException:
    conprintf("ERROR: Invalid String\n");
    end;
    }
    // Console Output: -> "ERROR: Invalid Argument"
    end;

    getInfo local.name local.arg local.string:
    // NO ERROR:
    if (!local.name) {
    throw InvalidNameException; // <-- Throws to the specified label inside the "catch" block.
    // After "throw" the thread ends and nothing else is executed.
    }

    // ERROR: local.arg = NIL
    if (!local.arg) {
    throw InvalidArgumentException; // <-- Throws to the specified label inside the "catch" block.
    // After "throw" the thread ends and nothing else is executed.
    }

    // ERROR: local.string = NIL
    // This won't be returned, as the thread already ended in the previous "throw"
    if (!local.string) {
    throw InvalidStringException; // <-- Throws to the specified label inside the "catch" block.
    // After "throw" the thread ends and nothing else is executed.
    }
    end local.result;


    InvalidNameException:
    conprintf("This thread will not be executed, even when it has the same label as the one inside the 'catch' block.\n");
    end;
    InvalidArgumentException:
    conprintf("This thread will not be executed, even when it has the same label as the one inside the 'catch' block.\n");
    end;
    InvalidStringException:
    conprintf("This thread will not be executed, even when it has the same label as the one inside the 'catch' block.\n");
    end;


    Another shorter example:

    try {
    local.a = 100;
    local.b = 0
    if (local.a > local.b) {
    throw Exception;
    }
    } catch {
    Exception:
    println("ERROR: local.a > local.b");
    }
    // Console Output: -> "ERROR: local.a > local.b"


    You can take a look to the Date and Time API mod, and see how it works in a real script.

    As extra info, You can't find any try-catch exception handling in any of the default scripts in MOHAA and/or FAKK2
    The original devs never used it on the game.


    Sorry if this isn't very clear, i'm bad at explaining things

  2. #2
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,603

    Default

    What a find, Really nice catch mate(hihihihihi).
    I just have a few comments/justifications (not about your style ofc it's perfect ).
    Generally speaking, exceptions are used when you are calling functions that can't return a value or have really specific/long return argument count that it'd be better to use exceptions for.
    It also helps to create easy to understand code as all error handling cases(inside catches) are done away from main code(inside tries).
    - You can only have one catch{} statements after each try{}
    Normally, in cpp you can have multiple catches after every try, but that's for catching different types of exceptions.
    That's why there's no need to chain catches in morpheus, we would just need to add a new exception/label.

    As extra info, You can't find any try-catch exception handling in any of the default scripts in MOHAA and/or FAKK2
    The original devs never used it on the game.
    That's because, funny enough, they were basically learning CPP and OOP at the time, so try-catching seemed like a no-no for them.
    "We can already return errors, why use exceptions !"

    and finally:
    Can we pass params/args to exceptions ?

  3. #3

    Default

    Quote Originally Posted by RyBack View Post
    Can we pass params/args to exceptions ?
    Just made a test, and it looks like it depends on whether the "throw" is located inside or outside the "try" block
    For example:

    This works:

    main:
    try {
    local.a = 100;
    local.b = 0
    if (local.a > local.b) {
    throw Exception local.a;
    }
    } catch {
    Exception local.arg:
    println("ERROR: " + local.arg);
    }
    // Console Output:
    // ERROR: 100
    end;


    This doesn't work:

    main:
    try {
    local.name = "UnnamedSoldier";
    local.arg = NIL;
    local.string = NIL;

    local.result = waitthread getInfo local.name local.arg local.string;
    } catch {
    InvalidArgumentException local.name:
    println("ERROR: " + local.name);
    }
    // Console Output:
    // ^~^~^ Script Error: binary '+' applied to incompatible types 'const string' and 'NIL'
    // NIL
    end;

    getInfo local.name local.arg local.string:
    if (!local.arg) {
    throw InvalidArgumentException local.name;
    }
    end local.result;


    any thoughts?

  4. #4
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Good find in deed. I think more scripts should use this technique to prevent issues... Granted it sounds like the functionality is a bit limited, but maybe with open mohaa src, we can improve upon it so that it functions better.

  5. #5

    Default

    This works because is the right way to do the try catch function if you look on C++ website C++ Try Catch/

    But have a limitation, you cant check on the "Catch" a variable.

    Quote Originally Posted by Zappa View Post


    main:
    try {
    local.a = 100;
    local.b = 0
    if (local.a > local.b) {
    throw Exception local.a;
    }
    } catch {
    Exception local.arg:
    println("ERROR: " + local.arg);
    }
    // Console Output:
    // ERROR: 100
    end;

  6. #6

    Default

    @Zappa: Theres one command you have missed and i have see him today is the delaythrow.

  7. #7

    Default

    Quote Originally Posted by DoubleKill View Post
    But have a limitation, you cant check on the "Catch" a variable.
    I'm not sure what you mean.

    Quote Originally Posted by DoubleKill View Post
    @Zappa: Theres one command you have missed and i have see him today is the delaythrow.
    The documentation says "Internal usage", and it behaves the same way as "throw", so...

  8. #8

    Default

    Quote Originally Posted by Zappa View Post
    I'm not sure what you mean.
    If you look to this example in c++, theres a variable on the catch but on the game dont work because he dont compile:


    // exceptions
    #include <iostream>
    using namespace std;

    int main () {
    try
    {
    throw 20;
    }
    catch (int e)
    {
    cout << "An exception occurred. Exception Nr. " << e << '\n';
    }
    return 0;
    }

  9. #9

    Default

    Quote Originally Posted by DoubleKill View Post
    If you look to this example in c++, theres a variable on the catch but on the game dont work because he dont compile:


    // exceptions
    #include <iostream>
    using namespace std;

    int main () {
    try
    {
    throw 20;
    }
    catch (int e)
    {
    cout << "An exception occurred. Exception Nr. " << e << '\n';
    }
    return 0;
    }
    but Morpheus isn't C++?
    I already replied to Ryback that is possible to pass arguments under certain circumstances.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •