KeeVeeGames / Exception.gml

Base class for the GameMaker Exceptions providing better output and handy code interface. Also imroves YYC support of Exceptions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GameMaker Exception Base Class

Donate License

This is a base class for custom exceptions. It replicates a structure of system exceptions and adding better support of try-catch and exception_unhandled_handler for these custom exceptions.

The class is generating all the necessary exception fields and populates data for script, line and stacktrace ones. Makes output on error windows nicer and more meaningful on handled exceptions. Also adds better support for YYC.

To create a custom exception inherit your constructor from Exception, write your message and longMessage and add init() call:

function TestException() : Exception() constructor {
    message = "Throw a test exception.";
    longMessage = "Long\nMessage\nis\nhere";
    
    init();
}

You can also not set both messages, which will make it hold just the exception class name, or make messages the same. Arguments are also supported:

function ArgumentException(expected_number, given_number) : Exception() constructor {
    message = string("Number of arguments expected {0}, got {1}", expected_number, given_number);
    longMessage = message;
    
    init();
}

Installation:

Copy the Exception script into your project.
Or get the latest asset package from the releases page and import it into IDE.

Comparison:

In GameMaker there are differences in error message appearance and exception data between different handling methods (try-catch or exception_unhandled_handler) and compile targets (VM or YYC).

This implementation tries to produce generally better and more consistent results in comparison to its alternatives.

Simple throw ("ArgumentException")

VM YYC
Unhandled

Error screenshot

Error screenshot

try-catch "ArgumentException"           "ArgumentException"          

exception
unhandled
handler

message: "Unable to find a handler for exception ArgumentException"
longMessage:

"ERROR in
action number 1
of Step Event0
for object obj_exception_test:

Unable to find a handler for exception ArgumentException

at gml_Script_ExceptionTest (line 66) - throw "ArgumentException""

script: "gml_Script_ExceptionTest"
line: 66
stacktrace: [

"gml_Script_ExceptionTest (line 66)"

"gml_Object_obj_exception_Step_0 (line 8) - ExceptionTest();"

]

"ArgumentException"

Custom throw new ArgumentException()

VM YYC
Unhandled

Error screenshot

Error screenshot

try-catch

message: "ArgumentException"
longMessage:

"ArgumentException
Number of arguments expected 3, got 2"

script: "gml_Script_ExceptionTest"
line: 65
stacktrace: [

"gml_Script_ExceptionTest (line 65)"

"gml_Object_obj_exception_Step_0 (line 8)

]

message: "ArgumentException"
longMessage:

"Unable to find a handler for exception ArgumentException
Number of arguments expected 3, got 2"

gml_Script_ExceptionTest (line 66)
gml_Object_obj_exception_Step_0 (line 9)

script: "gml_Script_ExceptionTest"
line: 65
stacktrace: [

"gml_Script_ExceptionTest (line 65)"

"gml_Object_obj_exception_test_Step_0 (line 8)

]

exception
unhandled
handler

message: "Unable to find a handler for exception ArgumentException
Number of arguments expected 3, got 2" longMessage:

"ERROR in
action number 1
of Step Event0
for object obj_exception_test:

Unable to find a handler for exception ArgumentException Number of arguments expected 3, got 2

at gml_Script_ExceptionTest (line 65) - throw new ArgumentException(3, 2);"

script: "gml_Script_ExceptionTest"
line: 65
stacktrace: [

"gml_Script_ExceptionTest (line 65)"

"gml_Object_obj_exception_test_Step_0 (line 8) - ExceptionTest();"

]

message: "ArgumentException: Number of arguments expected 3, got 2"
longMessage:

"Unable to find a handler for exception ArgumentException
Number of arguments expected 3, got 2

gml_Script_ExceptionTest (line 66)
gml_Object_obj_exception_test_Step_0 (line 9)

script: "gml_Script_ExceptionTest"
line: 66
stacktrace: [

"gml_Script_ExceptionTest (line 66)"

"gml_Object_obj_exception_test_Step_0 (line 9)"

]

Author:

Nikita Musatov - MusNik / KeeVee Games

License: MIT

About

Base class for the GameMaker Exceptions providing better output and handy code interface. Also imroves YYC support of Exceptions.

License:MIT License


Languages

Language:Game Maker Language 100.0%