- 1AclNotFoundException in Java
- 2ActivationException in Java
- 3AlreadyBoundException in Java
- 4ApplicationException in Java
- 5AWTException in Java
- 6BackingStoreException in Java
- 7BadAttributeValueExpException in Java
- 8BadBinaryOpValueExpException in Java
- 9BadLocationException in Java
- 10BadStringOperationException in Java
- 11BrokenBarrierException in Java
- 12CertificateException in Java
- 13CloneNotSupportedException in Java
- 14DataFormatException in Java
- 15DatatypeConfigurationException in Java
- 16DestroyFailedException in Java
- 17ExecutionException in Java
- 18ExpandVetoException in Java
- 19FontFormatException in Java
- 20GeneralSecurityException in Java
- 21GSSException in Java
- 22IllegalClassFormatException in Java
- 23InterruptedException in Java
- 24IntrospectionException in Java
- 25InvalidApplicationException in Java
- 26InvalidMidiDataException in Java
- 27InvalidPreferencesFormatException in Java
- 28InvalidTargetObjectTypeException in Java
- 29IOException in Java
- 30JAXBException in Java
- 31JMException in Java
- 32KeySelectorException in Java
- 33LambdaConversionException in Java
- 34LastOwnerException in Java
- 35LineUnavailableException in Java
- 36MarshalException in Java
- 37MidiUnavailableException in Java
- 38MimeTypeParseException in Java
- 39NamingException in Java
- 40NoninvertibleTransformException in Java
- 41NotBoundException in Java
- 42NotOwnerException in Java
- 43ParseException in Java
- 44ParserConfigurationException in Java
- 45PrinterException in Java
- 46PrintException in Java
- 47PrivilegedActionException in Java
- 48PropertyVetoException in Java
- 49ReflectiveOperationException in Java
- 50RefreshFailedException in Java
- 51RemarshalException in Java
- 52RuntimeException in Java
- 53SAXException in Java
- 54Java ScriptException
- 55Java ServerNotActiveException
- 56Java SOAPException
- 57Java SQLException
- 58Java TimeoutException
- 59Java TooManyListenersException
- 60Java TransformerException
- 61Java TransformException
- 62Java UnmodifiableClassException
- 63Java UnsupportedAudioFileException
- 64Java UnsupportedCallbackException
- 65Java UnsupportedFlavorException
- 66Java UnsupportedLookAndFeelException
- 67Java URIReferenceException
- 68Java URISyntaxException
- 69Java UserException – Custom Exceptions with Examples
- 70Java XAException
- 71Java XMLParseException – XML Parsing and Exception Handling
- 72Java XMLSignatureException
- 73Java XMLStreamException – StAX Parsing Examples
- 74Java XPathException – Complete Guide with Examples
Java PropertyVetoException
Introduction to PropertyVetoException in Java
JavaBeans is a component-based architecture used for creating reusable software components. In this architecture, bean properties can be changed at runtime. But sometimes, changes to these properties might be restricted by business logic or validation rules. When a change is not allowed, Java uses a mechanism to veto the change—and that's where PropertyVetoException
comes in.
In this beginner-friendly tutorial, we’ll walk through what PropertyVetoException
is, why and when it is thrown, and how to build your own JavaBeans that can accept or reject property changes. We’ll explain everything using relatable examples involving apples, bananas, and cherries—so you can learn with flavor and fun.
What is PropertyVetoException?
PropertyVetoException
is a checked exception in the java.beans
package. It is thrown to indicate that a proposed change to a property has been vetoed by a registered listener. This is part of the JavaBeans mechanism where objects can reject property updates by listening to property change events.
Class Hierarchy
java.lang.Object
↳ java.lang.Throwable
↳ java.lang.Exception
↳ java.beans.PropertyVetoException
Key Interfaces and Classes
VetoableChangeListener
– Listens for and can veto property changesPropertyChangeEvent
– Describes a property changePropertyVetoException
– Thrown to veto a change
Example Scenario
Imagine we have a JavaBean called Fruit
with a property color
. We want to allow only certain colors (e.g., red, yellow, green), and reject others like purple or black. If someone tries to set an invalid color, a veto should occur.
Example 1: A Simple JavaBean with VetoableChangeListener
import java.beans.*;
public class Fruit {
private String color;
private final VetoableChangeSupport vetoSupport = new VetoableChangeSupport(this);
public void setColor(String newColor) throws PropertyVetoException {
String oldColor = this.color;
vetoSupport.fireVetoableChange("color", oldColor, newColor);
this.color = newColor;
System.out.println("Color successfully set to: " + newColor);
}
public String getColor() {
return color;
}
public void addVetoableChangeListener(VetoableChangeListener listener) {
vetoSupport.addVetoableChangeListener(listener);
}
}
Example 2: Adding a Veto Logic
public class FruitColorValidator {
public static void main(String[] args) {
Fruit apple = new Fruit();
apple.addVetoableChangeListener(evt -> {
String newColor = (String) evt.getNewValue();
if (!newColor.equalsIgnoreCase("red") &&
!newColor.equalsIgnoreCase("green") &&
!newColor.equalsIgnoreCase("yellow")) {
throw new PropertyVetoException("Invalid fruit color: " + newColor, evt);
}
});
try {
apple.setColor("red");
apple.setColor("purple"); // This will be vetoed
} catch (PropertyVetoException e) {
System.out.println("Vetoed: " + e.getMessage());
}
System.out.println("Final fruit color: " + apple.getColor());
}
}
Output
Color successfully set to: red
Vetoed: Invalid fruit color: purple
Final fruit color: red
Explanation
In the above example:
- We created a
Fruit
JavaBean with acolor
property - We added a
VetoableChangeListener
that only allows red, green, or yellow - When "purple" is passed, the listener throws
PropertyVetoException
, rejecting the change
Why Use PropertyVetoException?
This mechanism is powerful in enterprise-level applications where one component must approve changes made by others—especially in GUIs, configuration systems, or workflows where changes must be validated before acceptance.
Best Practices
- Always validate inputs inside your
VetoableChangeListener
- Catch and handle
PropertyVetoException
where the property is set - Provide meaningful messages to help users understand why a change was rejected
- Keep veto logic focused on rules, not on presentation or logging logic
Common Mistakes and Fixes
Mistake | Fix |
---|---|
Not throwing exception in listener when needed | Use throw new PropertyVetoException() when validation fails |
Not wrapping property setting in try-catch | Catch PropertyVetoException to handle rejections gracefully |
Forgetting to call fireVetoableChange() |
Ensure fireVetoableChange() is invoked before changing property |
Real-World Analogy
Think of a fruit market manager who reviews every change to the fruit label. If someone tries to label a banana as “blue,” the manager immediately stops them and says “Vetoed!”—just like PropertyVetoException
stopping a change in your JavaBean.
Recap
PropertyVetoException
is thrown when a change to a bean property is vetoed- It is part of JavaBeans' powerful change-listening system
- You can use
VetoableChangeSupport
to manage listeners - Validate changes inside
VetoableChangeListener
and throw exceptions when needed
Conclusion
JavaBeans gives you great control over component behavior, and PropertyVetoException
is one of its powerful tools. It enables real-time validation and control over state changes.
Comments
Loading comments...