- Amy Winehouse
#===================================================================================================
# Example Script: Aztec Simple Exception Script
# Demonstrates the use of exception handling in the thread where the exception occurred.
#===================================================================================================
# Main run-time entry point for the script.
method Main
{
data<bool> Error
data<bool> ForceMathException
data<bool> ForceNullException
data<float> FinalResult
data<SampleAnalysis> MyDataAnalysis
# Look at 1st command line arg and set "force exception" flags if 1 or 2.
if ( GetScript().GetArg(1) == '1' )
ForceMathException = true
else if ( GetScript().GetArg(1) == '2' )
ForceNullException = true
#--------------------------------------------------------------
# Wrap all script processing in a catch-all exception handler
# and also look specifically for 'NullRefException'.
#--------------------------------------------------------------
exceptions
{
# Create sample class and call the calculate method to possibly cause an exception.
MyDataAnalysis = new<SampleAnalysis(25.0,ForceMathException,ForceNullException)>
FinalResult = MyDataAnalysis.CalculateResult(2.5)
}
handle<NullRefException Exception>
{
Error = true
StdIO.Write("Handled a NullRef exception in 'Main()' that came from 'CalculateResult()'.")
}
handle<ExceptionEvent Exception>
{
# All as-of-yet unhandled exceptions will come this way before script exits.
Error = true
StdIO.Write(Exception.ObjectClass().Name() + " occurred at line " + Exception.ModuleLine().Str())
}
cleanup
{
if ( !Error )
{
StdIO.Write("No exceptions handled in Main. Final result is " + FinalResult.Str())
}
}
}
# Simple class used for exception demonstration.
class SampleAnalysis
{
method SampleAnalysis(float InitValue, bool ForceMathException = false, bool ForceNullException = false)
{
InitialValue = InitValue
self.ForceMathException = ForceMathException
self.ForceNullException = ForceNullException
}
method<float> CalculateResult(float Input)
{
data<float> Output
data<Thread> ThisThread
#--------------------------------------------------------------------------------
# Wrap the processing for this method in an exceptions/handle construct. For
# demonstration purposes, we'll handle Math here and let Null go back to caller.
#--------------------------------------------------------------------------------
exceptions<MathException>
{
# Calculate a dummy result. If flag is set to cause exception, initiate it by dividing by zero.
if ( ForceMathException )
{
Input = 0
}
Output = InitialValue / Input
# Initiate a "Null" exception if flag is set by calling a method with a null reference.
if ( !ForceNullException )
{
ThisThread = GetThread()
}
StdIO.Write("Thread id is " + ThisThread.ThreadId().Str())
}
handle
{
# We know this is a Math exception since that's the only thing we filtered on above.
StdIO.Write("Handled a math exception in the 'CalculateResult()' method.")
}
return(Output)
}
data<bool> ForceMathException
data<bool> ForceNullException
data<float> InitialValue
}