mbourd
mbourd

Reputation: 55

Exception in compiled script

I'd like to share an issue that the function Exception(Message [, What, Extra]) doesn't work as expected if the script is compiled in .exe

class MyClass
{
    Method1() 
    {
         MsgBox % MyClass.Method2()
    }

    Method2()
    {
         return Exception("", -2).What
    }
}

MyClass.Method1()

In non compiled MyClass.Method1() display MyClass.Method1

But in compiled .exe script MyClass.Method1() display -2

Anyone has a solution to display the method instead the number if the script is compiled?

Upvotes: 0

Views: 43

Answers (1)

0x464e
0x464e

Reputation: 6489

It actually does work as expected, as this behavior is stated in the documentation:

If What is omitted, it defaults to the name of the current function or subroutine. Otherwise it can be a string or a negative offset from the top of the call stack. For example, a value of -1 sets Exception.What to the current function or subroutine, and Exception.Line and Exception.File to the line and file which called it. However, if the script is compiled or the offset is invalid, What is simply converted to a string.

Also, I think your implementation here is a bit weird, surely you'd want to throw exceptions instead of returning them?

Anyway, I guess your solution would be to somehow implement this yourself.
You could for example pass the caller in as an argument:

instance := new MyClass

try
    instance.Method1()
catch e
    MsgBox, % e.What


class MyClass
{
    Method1() 
    {
        this.Method2(A_ThisFunc)
    }

    Method2(caller)
    {
        throw Exception("", caller)
    }
}

Upvotes: 1

Related Questions