It would be nice to have something like try-catch statement in E2.
For example:
try {
error("Test")
} catch (Error:string) {
print("Exception has been thrown (" + Error + ")!") # Would print "Exception has been thrown (Test)!"
}
No, e2 is designed to be simple and error free.
Agreeing with @thegrb93, E2 can't randomly produce errors as it doesn't have functions that would work on one occasion, but error on another
What about divide by zero?
That doesn't cause an error, and most functions that take numbers that matter have checks against it.
Now that is confusing.. But, what's the point of having void=error(s) (core) function then?
So you can purposely error the E2? Usually I use it if I'm giving out an E2 and the person who sets it up uses it wrong, gives them a nice error.
E2 already has an error(S) function, which causes the whole chip to crash on purpose.
E2 doesn't have try/catch because it's made to never crash unless it's very serious (such as running out of operations). Every function has validity checks for this reason. For example, if you give an invalid entity to any entity manipulation function, you won't crash the E2.
The wonders of strict typing make errors a nonissue, but it would be nice if when you try to do something like call a function on a nonexistent entity (entity(-1):applyForce(V)), E2 would throw a warning into your console and maybe make a beep (with a throttle or debounce), so you know you did something wrong and can go fix it. Huzzah for better (possibly faster) code!
Besides nonexistent entities/wirelinks and popping/removing an element of an array/table that isn't there, there isn't much more I can think to warn about.
Anyone else work with MySQL or PHP? They both have fatal errors (besides syntax) and warnings.
@Divran No, no.. Well, I didn't mean a "classic" try-catch statement, but an E2 try-catch..
For example (like a wrapper around a function which would return an exception ID that is passed afterwards to the catch callback). Here is mine simplified (alpha) version of try-catch mechanism:
# From "default" E2 library:
@persist DEFAULT_TABLE:table
function table defaultTable() { return DEFAULT_TABLE }
#region User-Code
function number testFunction()
{
# Do something . . .
print("In testFunction()")
# Return a number (exception ID) other than 0 - means something went unsuccessful.
return 1 # 0=Success, 1="NotImplemented" exception will be thrown.
}
### <summary>A callback for try-catch around testFunction. Called once an exception is thrown.</summary>
### <param name="ExceptionData">Contains information about an exception (state).</param>
function number delegate_try__catch__testFunction__0001(ExceptionData:table)
{
print("In catch block of testFunction(). Catched and handled '" + ExceptionData["Error", string] + "' exception!")
return 1 # We have handled the exception successfully, if not then return 0 instead, so E2 will throw an error.
}
#endregion User-Code
# Exception Name = Exception ID and vice-versa (will be auto-generated enumeration...)
Exception_Enum = table("1" = "NotImplemented", "NotImplemented" = 1) # Simplified enum version. Exception enumeration table.
#region Simple Try-Catch mechanism
function table throw(ExceptionID)
{
return table("ExceptionID" = ExceptionID, "Error" = Exception_Enum[ExceptionID:toString(), string])
}
function table throw(ExceptionID, Error:string)
{
return table("ExceptionID" = ExceptionID, "Error" = Error)
}
function table throw(Error:string)
{
return table("ExceptionID" = Exception_Enum[Error, number], "Error" = Error)
}
function table try(TryFunction:string)
{
local Result = TryFunction()[number]
if (Result)
{
local ExceptionResult = throw(Result)
ExceptionResult["Function", string] = TryFunction
return ExceptionResult
}
return defaultTable()
}
function table:catch(CatchDelegate:string)
{
if (!This["Error", string])
{
return
}
if (!CatchDelegate)
{
error(This["Error", string] + " exception has been thrown (main)!")
}
local CatchResult = CatchDelegate(This)[number]
#print("")
#hint("", 7)
#soundPlay("", 0.0, "")
if (!CatchResult)
{
error(This["Error", string] + " exception has been thrown (" + (This["Function", string] ?: "unknown") + ")!")
}
}
#endregion Simple Try-Catch mechanism
#
# Test Section:
#
# Force-throw.
throw("NotImplemented"):catch("")
# Try-Catch.
try(
"testFunction"
):catch(
"delegate_try__catch__testFunction__0001"
)
Looks like you already have it solved inside E2 itself
On Tue, Jun 14, 2016 at 4:04 AM, CaptainPRICE [email protected]
wrote:
@Divran https://github.com/Divran No, no.. Well, I didn't mean a
"classic" try-catch statement, but an E2 try-catch..
For example (like a wrapper around a function which would return an
exception ID that is passed afterwards to the catch callback). Here is mine
simplified (alpha) version of try-catch mechanism:From "default" E2 library:
@persist DEFAULT_TABLE:table
function table defaultTable() { return DEFAULT_TABLE }region User-Code
function number testFunction()
{
# Do something . . .
print("In testFunction()")
# Return a number (exception ID) other than 0 - means something went unsuccessful.
return 1 # 0=Success, 1="NotImplemented" exception will be thrown.
}
A callback for try-catch around testFunction. Called once an exception is thrown.
Contains information about an exception (state).
function number delegate_try__catch__testFunction__0001(ExceptionData:table)
{
print("In catch block of testFunction(). Catched and handled '" + ExceptionData["Error", string] + "' exception!")
return 1 # We have handled the exception successfully, if not then return 0 instead, so E2 will throw an error.
}endregion User-Code
Exception Name = Exception ID and vice-versa (will be auto-generated enumeration...)
Exception_Enum = table("1" = "NotImplemented", "NotImplemented" = 1) # Simplified enum version. Exception enumeration table.
region Simple Try-Catch mechanism
function table throw(ExceptionID)
{
return table("ExceptionID" = ExceptionID, "Error" = Exception_Enum[ExceptionID:toString(), string])
}function table throw(ExceptionID, Error:string)
{
return table("ExceptionID" = ExceptionID, "Error" = Error)
}function table throw(Error:string)
{
return table("ExceptionID" = Exception_Enum[Error, number], "Error" = Error)
}function table try(TryFunction:string)
{
local Result = TryFunction()[number]
if (Result)
{
local ExceptionResult = throw(Result)
ExceptionResult["Function", string] = TryFunction
return ExceptionResult
}
return defaultTable()
}function table:catch(CatchDelegate:string)
{
if (!This["Error", string])
{
return
}
if (!CatchDelegate)
{
error(This["Error", string] + " exception has been thrown (main)!")
}
local CatchResult = CatchDelegate(This)[number]
#print("")
#hint("", 7)
#soundPlay("", 0.0, "")
if (!CatchResult)
{
error(This["Error", string] + " exception has been thrown (" + (This["Function", string] ?: "unknown") + ")!")
}
}endregion Simple Try-Catch mechanism
#
Test Section:
#
Force-throw.
throw("NotImplemented"):catch("")
Try-Catch.
try(
"testFunction"
):catch(
"delegate_try__catch__testFunction__0001"
)—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/wiremod/wire/issues/1141#issuecomment-225760615, or mute
the thread
https://github.com/notifications/unsubscribe/ABaroIoE-td53-KzsBsyspeeBdFoDRwPks5qLgwlgaJpZM4IzEdq
.