private static void checkLastError() throws Throwable {
int errCode = (int)(long) NativeLib.{{ prefix_upper }}_LAST_ERROR_CODE.invoke();
if (errCode != 0) {
var ctxPtr = (MemorySegment) NativeLib.{{ prefix_upper }}_LAST_ERROR_CONTEXT.invoke();
String msg = ctxPtr.equals(MemorySegment.NULL)
? "Native call failed with error code " + errCode
: ctxPtr.reinterpret(Long.MAX_VALUE).getString(0);
throw NativeLib.lastErrorException(errCode, msg);
}
}