I'm not sure about JAVA in general but it certainly won't let you create a short MIDI message outside of a catch/try block so perhaps it's up to the class developer to decide on the compile behaviour.
EditComponentValue()
-
- Posts: 140
- Joined: Sun Jan 22, 2023 5:18 am
- Location: Melbourne
- Contact:
Re: EditComponentValue()
Re: EditComponentValue()
I'm wondering how this works exactly.Centripidity wrote: ↑Tue Feb 07, 2023 1:06 pm perhaps it's up to the class developer to decide on the compile behaviour.
I've looked at the javac options...
https://docs.oracle.com/javase/7/docs/t ... javac.html
...and there doesn't appear to be anything obvious there, so I'm figuring this must be language level.
On the runtime options I've quickly scanned through the -switches and there's nothing that leaps out about what is different between VMD debug mode, VMD normal mode and VM proper mode. But there's a fair amount of detail in there so no doubt I've missed something.
https://docs.oracle.com/javase/8/docs/t ... /java.html
- honki-bobo
- Posts: 305
- Joined: Sat Nov 09, 2019 1:18 pm
Re: EditComponentValue()
The behavior of a JAVA program in case of an exception depends on at least two things:
1) Is it a checked or an unchecked exception?
An unchecked exception is a java.lang.RuntimeException or java.lang.Error and all of their respective subclasses, like NullPointerException, IndexOutOfBoundsException or NumberFormatException. The compiler does not require that you catch or specify runtime exceptions (although you can).
You can find more information in the JAVA documentation.
An InvalidMidiDataException inherits from java.lang.Exception and is not a descendant of java.lang.RuntimeException and is therefore a checked exception that needs to be handled.
2) Is the exception handled further up the call stack?
I don't have any information about how VM handles exceptions outside of a module, but I would suppose that exceptions are handled in some form on a higher level to prevent the JVM from crashing.
So if you want to avoid NumberFormatExceptions popping up in your logs you will need to ensure that newText can actually be parsed to a double in EditComponentValue before passing it on to the call to super.EditComponentValue.
Hope this helps.
Best regards,
Martin
1) Is it a checked or an unchecked exception?
An unchecked exception is a java.lang.RuntimeException or java.lang.Error and all of their respective subclasses, like NullPointerException, IndexOutOfBoundsException or NumberFormatException. The compiler does not require that you catch or specify runtime exceptions (although you can).
You can find more information in the JAVA documentation.
An InvalidMidiDataException inherits from java.lang.Exception and is not a descendant of java.lang.RuntimeException and is therefore a checked exception that needs to be handled.
2) Is the exception handled further up the call stack?
I don't have any information about how VM handles exceptions outside of a module, but I would suppose that exceptions are handled in some form on a higher level to prevent the JVM from crashing.
So if you want to avoid NumberFormatExceptions popping up in your logs you will need to ensure that newText can actually be parsed to a double in EditComponentValue before passing it on to the call to super.EditComponentValue.
Hope this helps.
Best regards,
Martin
Re: EditComponentValue()
Thanks Martin, that explains the unchecked/checked thing. I figured there must be a relatively straightforward answer!
But on the EditComponentValue format execption thing you can't fix it by passing a valid newText string to super.EditComponentValue().
I just tested this by passing "1" to the superclass via newText.
If you run this in debug mode in VMD it works as expected if the string is a valid double but throws an exception otherwise.
Therefore the exception must be being thrown BEFORE a module's EditComponentValue() is called not after.
But on the EditComponentValue format execption thing you can't fix it by passing a valid newText string to super.EditComponentValue().
I just tested this by passing "1" to the superclass via newText.
Code: Select all
@Override
public void EditComponentValue( VoltageComponent component, double newValue, String newText )
{
// add your own code here
newText = "1";
super.EditComponentValue( component, newValue, newText );
}
Therefore the exception must be being thrown BEFORE a module's EditComponentValue() is called not after.
Re: EditComponentValue()
This whole thing is a bit strange really as although one reason for using EditComponentValue() is to enable us to do some math to say change the range or law of a number it also works if you pass arbitary text like a note name providing you aren't running in debug mode. And that is presumably the intention else why pass the string as a parameter?
Also it doesn't make sense for the superclass method to parse newText as what would reparsing the string do if you've already parsed it and changed newValue?
Yet you have to call the superclass method to pass the newValue.
I just did another test and if you pass garbage to the superclass method via newText it doesn't throw an exception so the newText parameter is simply being ignored in the superclass method.
My conclusion is that the whole thing works something like this...
With the format exception from ParseDouble() being logged but not thrown when not in debugging mode.
Edited to say, I know EditComponentValue() doesn't return anything but it was more elegant to express the idea this way.
Also it doesn't make sense for the superclass method to parse newText as what would reparsing the string do if you've already parsed it and changed newValue?
Yet you have to call the superclass method to pass the newValue.
I just did another test and if you pass garbage to the superclass method via newText it doesn't throw an exception so the newText parameter is simply being ignored in the superclass method.
My conclusion is that the whole thing works something like this...
Code: Select all
void editValue( String userString )
{
component.SetValue( component.EditComponentValue( Double.parseDouble( userString ), userString ) );
}
Edited to say, I know EditComponentValue() doesn't return anything but it was more elegant to express the idea this way.
Last edited by ColinP on Tue Feb 07, 2023 5:06 pm, edited 2 times in total.
- honki-bobo
- Posts: 305
- Joined: Sat Nov 09, 2019 1:18 pm
Re: EditComponentValue()
That's a very interesting find!
Correct me if I'm wrong here, but that would mean that the NumberFormatException must be occurring in the implementation of the VoltageKnob or VoltageComponent class and there is basically nothing a developer can do about it.
What's also curious is that in the stack trace of the NumberFormatException the highest (or lowest depending on your view) entry is voltage.core.Values.ParseStringToDouble and not VoltageKnob or the module class.
Correct me if I'm wrong here, but that would mean that the NumberFormatException must be occurring in the implementation of the VoltageKnob or VoltageComponent class and there is basically nothing a developer can do about it.
What's also curious is that in the stack trace of the NumberFormatException the highest (or lowest depending on your view) entry is voltage.core.Values.ParseStringToDouble and not VoltageKnob or the module class.
Re: EditComponentValue()
Yup, basically it's an untrappable problem exhibited by every VM module that until Centripidity found it hadn't been noticed because it doesn't have any serious fallout outside debugging mode beyond causing a log entry.
-
- Posts: 588
- Joined: Mon Nov 15, 2021 9:23 pm
Re: EditComponentValue()
Just out of curiosity, is there some kind of unmentioned requirement that newValue has to be equivalent to the newText string? That might account for one of the exceptions Colin saw in his testing.
Reid
Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
- honki-bobo
- Posts: 305
- Joined: Sat Nov 09, 2019 1:18 pm
Re: EditComponentValue()
@Reid: Doesn't seem like it. I did a quick test by adding a single knob to an otherwise empty module and changed the newText like so:
This didn't cause any issues (except the already discovered NumberFormatException when entering values that cannot be parsed to a double).
Code: Select all
public void EditComponentValue( VoltageComponent component, double newValue, String newText )
{
// add your own code here
newText = "" + (newValue + 0.1);
super.EditComponentValue( component, newValue, newText );
}