Skip to content

Throw for bad param in GetParameterValue#465

Open
garfieldnate wants to merge 1 commit into
developmentfrom
451-exceptions
Open

Throw for bad param in GetParameterValue#465
garfieldnate wants to merge 1 commit into
developmentfrom
451-exceptions

Conversation

@garfieldnate
Copy link
Copy Markdown
Collaborator

Previous behavior here was to segfault! An exception is much more user-friendly.

Add the SWIG machinery for catching thrown exceptions in the SML binding libraries, too. We wrap every SML function with a try/catch. I don't know if this is actually heavy or not, but at least one implementation out there thought it was and went an alternative route where each method has to be explicitly marked to catch exceptions:
https://github.com/KiCad/kicad-source-mirror/blob/47e4ebb32a1366d60649879381eac819f7c7131d/common/swig/ki_exception.i#L41

Add tests for TCL, Python and Java that demonstrate the exceptions being translated for each host language. We don't have C# tests yet T_T.

Notice we did have to add the atexit handler back to prevent a segfault when the exception is not caught correctly; I don't know exactly why. Filed #464.

Fixes #451.

Previous behavior here was to segfault! An exception is much more user-friendly.

Add the SWIG machinery for catching thrown exceptions in the SML binding
libraries, too. We wrap every SML function with a try/catch. I don't know if
this is actually heavy or not, but at least one implementation out there thought
it was and went an alternative route where each method has to be explicitly
marked to catch exceptions:
https://github.com/KiCad/kicad-source-mirror/blob/47e4ebb32a1366d60649879381eac819f7c7131d/common/swig/ki_exception.i#L41

Add tests for TCL, Python and Java that demonstrate the exceptions being
translated for each host language. We don't have C# tests yet T_T.

Notice we did have to add the `atexit` handler back to prevent a segfault when
the exception is not caught correctly; I don't know exactly why. Filed #464.

Fixes #451.
SWIG_exception(SWIG_RuntimeError, e.what());
}
catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
Copy link
Copy Markdown
Contributor

@ShadowJonathan ShadowJonathan May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still maybe append the exception type / value / string to give at least some information to the developer, imo.

class StringElement ;
class WorkingMemory ;
class Identifier ;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these whitespace changes btw

scijones added a commit that referenced this pull request Apr 15, 2026
…#465)

Cherry-pick of garfieldnate's PR #465. Changes GetParameterValue to throw
std::invalid_argument instead of returning NULL (which caused segfaults).
Adds SWIG %exception block to translate C++ exceptions to language-native
exceptions (ValueError in Python, IllegalArgumentException in Java, etc.).
Includes tests for Python, Java, and Tcl.

Fixes #451.
Co-authored-by: garfieldnate <garfieldnate@gmail.com>
scijones added a commit that referenced this pull request Apr 21, 2026
Follow-up to the revert of PR #465 (commit 828c80c). Re-lands the
diagnostic improvement requested in issue #451 -- tell the user which
parent+attribute was missing -- but uses std::cerr + NULL return instead
of throwing std::invalid_argument. This matches moschmdt's own alternate
suggestion in #451 ('replace the throw with a print command, e.g. cout,
and do not throw an error to remain API compatible') and garfieldnate's
acknowledgement that 'returning null and printing to cerr would be very
easy; we already print to cerr throughout Soar'. Avoids breaking existing
callers (and SWIG/Java consumers where the thrown exception was getting
silently swallowed by SML event-handler frames). Also keeps the
trailing-whitespace cleanup from the reverted commit.

Refs #451, #465.
scijones added a commit that referenced this pull request Apr 21, 2026
…eturn

The cerr output still produces too much noise in agents that legitimately
probe for optional attributes. Full rollback to pre-#465 behavior.
Whitespace cleanup from 828c80c is preserved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SML: Segfault when accessing null pointer without error message

2 participants