Code Monkey home page Code Monkey logo

exception.gml's Introduction

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

exception.gml's People

Contributors

keeveegames avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.