In case of an error or missing reference, I’d like to call something like: ReportError(currLine);. It will be very often put in a function, and I would like it to call a method that I point it to, and return; function that it’s in, so if a reference is missing, it will do it’s thing (ReportError) and then return; (i.e. end execution of this invoke) in the script that it’s called.
It’s not an issue, but it will make code far cleaner.
If you mean, how do you report an “error” without throwing an exception, create a new instance of an exception and don’t throw it. The instance will be populated with line numbers. You would have to write return after every ReportError.
First solution will turn my two liner into multiple liner.
Second solution will throw an error, in my case they’re warnings, not game breakers but something that should return any execution.
I was hoping there’s a way to add these (to me) very cryptic tags that programmers have next to/inside methods, and have it execute itself and stop the execution of function that it’s residing in.
Apparently there’s a way to create custom exceptions without spending 50 years on it.
Now I can throw my own function in there and do it, but now I have a new problem, not only my exception is thrown, but also the default one. So I have two things in console, a warning from my script and vanilla exception error. As idiotic as it may sound, is it possible to suppress the default one? It still needs to end execution of a method.
Suppress only this particular exception. I need others for debugging.
What do you mean by “not only my exception is thrown, but also the default one”?
As you’ve alresdy discovered, you can create your own exception types. If an exception is thrown, you’d either let it bubble up, or handle it.
If you intend to handle an exception by throwing your custom exception, your custom exception type should always have an overloaded constructor that takes an “innerException”, so that you don’t lose information about the original error.
Note that exceptions are not necessarily true exceptions. Some are truly exceptional, while others shouldn’t be handled at all because they’re thrown due to bugs in your code. The proper way to avoid the latter is to actually fix the code, or if a component is missing, the fix is to either add the component or let it be optional.
First of all, they’re different.
The first one looks like it simply logs a warning or an error. The second throws an exception.
The overall statement is a bit odd.
Let’s assume data is not null. (string)data[0] might already throw a cast exception, so this should only be done if you - either know that data array contains strings in general / at that index at least), - or you want it to throw that exception if it cannot be casted
or you don’t want it to fail at that point, in which case you’re better of using data[0] as string. This one is exception free, so that you’re guaranteed to either get your value as a string, or null.
Whatever you’ve chosen, what follows is:
When you get to the point that your custom exception is about to be thrown, which only happens when (string)data[0] == null, you throw your custom exception with the argument “null”, well, because you just verified it’s null.
Or is the argument supposed to be “data[0]”, that is, a string literal?
Yes, it’s shorter and might be perceived as a cleaner solution. But it is still different to the first example, as described above.
Then I have to make it even longer for each variable:
string myData = data[0] as string;
if (myData == null) {
WriteLog("Item missing");
return;
}
I was looking for a way to make it shorter not longer
The intent is that if it’s not what I want, (whatever the condition), it notifies external script and forces a return to prevent further execution. Someone above suggested exception, which I’m attempting to throw, and then you tell me it might backfire. So we’re back to square one.
But as I said, if you want the cast exception in case that slot is not null but contains anything else than a string, it’s perfectly valid to use (string)data[0].
That’s certainly good to know, but the post wasn’t made about issue of exceptions, rather executions.
Now I have:
if (Empty((string)data[0]) {
MissingVariable();
return;
}
into:
if (data[0] as string == null) {
MissingVariable();
return;
}
And whilst I’m very thankful that you notified me of this “as” feature (now my code will be way cleaner). The initial issue was whether I can trigger something that would execute a function and prevent further execution of code. The two above work fantastically. Here’s an example: say user finds themselves at an illegal position, in case it’s not fault of my programming, rather they hacked the game, I’d like to send a report and prevent execution of function. Obviously he could disable that function as well, but that’s just a hypothetical situation. This two-liner gets really ugly as I’m using it quite often to double check non-guaranteed (by compiler) variables. There’s multiple positions where such check would be logically valid and I was looking forward towards cleaning my code up, it doesn’t look great if I have 7 if statements on top a method (even if I collapse them), I would prefer them to be cut down to a simple if 7 one-liners. Exception almost does this, except that it prints default Exception as well.
if (data[0] as string == null) InvalidData("It's not a string!");
Function cannot precede without ALL of these variables being set and be what I want them to be.
I know
Just wanted to point it out, as it caught my attention.
In that case, you can just use what you had attempted to use in a previous post. Though, do not pass the variable you just verified as being null, but some useful information.
if (data[0] as string == null) throw new CustomException("Some useful information...");
Or if it takes some more lines to set up the exception, move it to a method (the second option you also used)
if (data[0] as string == null) ThrowUnexpectedValueException("data[0]");
private void ThrowUnexpectedValue(string missingValue)
{
// build your message from arguments / add some extra information
throw new UnexpectedValueException(missingValue);
}
You could also move the entire evaluation to the throw helper, but it takes quite a lot of methods if the evaluation is very specific.
Yes, exactly, this is the point where the issue is.
If my function “ThrowUnexpectedValue” has Debug.Log(string missingValue). In my console I will get both my own Debug.Log and default error. Literally [19:00:49] ThrowUnexpectedValue: null., but right above it in console you can also see my own command printing “Missing variable!”
For every single time I invoke it, I get two entries, one from my own script, and second as standard reaction. The question now is, whether I can suppress the default one in that particular throw.
I think you mentioned somewhere that that would take some time to program, is there at least a direction you could point me to?
For people in the future.
With my Uber-haxor 1337 engineering skills, I found out you can cast enums like this: var entity = (data[0] as Item?) ?? throw new Exception("Missing references");
You have to add nullable to enum and check if it throws.
I’ll add it once I’ll find out how to cast classes.
Edit: Rule of the thumb seems to be var entity = (data[0] as Item) ?? throw new Exception();
if it complains about it being non-nullable, just put ? after the Item.
It does throw an error and return (which is the first part), but it doesn’t allow me to invoke my function for my own purposes. I need more room to program my own functions to do something with this exception in my own standardized way.