08_Exception
Code-Dateien
| Dateiname | Aktion |
|---|---|
| CODECode_Bank.zip | Download |
| CODECode_Pizza.zip | Download |
| CODECode_Urlaub.zip | Download |
Videos
| Dateiname | Aktion |
|---|---|
| VIDEOVideo_Bank_D | Abspielen |
| VIDEOVideo_Pizza_E | Abspielen |
| VIDEOVideo_Urlaub_D | Abspielen |
Lernmaterialien
Exception
Button
OrdersView.java
private final Button buttonAddWrong = new Button("Add WRONG order");
public OrdersView(@Autowired PizzaOrderService pizzaOrderService) {} {
buttonAddWrong.addClickListener(b -> addWrongOrder());
}
private void addWrongOrder() {
pizzaOrderService.addWrongOrder();
reload();
}PizzaOrderService.java
public void addWrongOrder() {
PizzaOrder order;
// -20 Euro!!!
order = new PizzaOrder(LocalDate.now(), "Margherita", "Large", -20.0, 1, true);
orders.add(order);
}PizzaOrder.java
public void setPrice(Double price) throws PizzaOrderException {
if (price < 8)
throw new PizzaOrderException("The min. price is 8.0 Eur");
if (price > 30)
throw new PizzaOrderException("The max. price is 30.0 Eur");
this.price = price;
}PizzaOrderException.java
public class PizzaOrderException extends RuntimeException {
public PizzaOrderException(String message) {
super(message);
}
}Here is a short explanation of what your code does and where the issue is:
OrdersView (UI layer)
private final Button buttonAddWrong = new Button("Add WRONG order");- Creates a button in the UI.
buttonAddWrong.addClickListener(b -> addWrongOrder());- When the button is clicked → calls
addWrongOrder().
private void addWrongOrder() {
pizzaOrderService.addWrongOrder();
reload();
}Calls the service to add an invalid order
Then refreshes the Grid
PizzaOrderService (business logic)
order = new PizzaOrder(..., -20.0, ...);
orders.add(order);Creates a pizza order with price = -20 € ❌
Adds it to the list
👉 This is intentionally invalid data
PizzaOrder (validation)
public void setPrice(Double price) throws PizzaOrderException {
if (price < 8)
throw new PizzaOrderException("The min. price is 8.0 Eur");Validates the price
Throws an exception if:
price < 8
price > 30
👉 So -20 should trigger an exception
PizzaOrderException
public class PizzaOrderException extends RuntimeExceptionCustom exception
Extends
RuntimeException→ unchecked exceptionNo
try/catchrequired → app can crash if not handled
Error Message
OrdersView.java
private void addWrongOrder() {
try {
pizzaOrderService.addWrongOrder();
reload();
}
catch (PizzaOrderException e) {
Notification.show(e.getMessage());
}
}Method is called
This method is usually triggered by your button:
buttonAddWrong.addClickListener(b -> addWrongOrder());👉 User clicks → this method runs
try { ... } block
pizzaOrderService.addWrongOrder();Calls the service
Service tries to create an invalid order (price = -20)
👉 This may throw a PizzaOrderException
reload();Only runs if no exception occurred
Refreshes the Grid
catch (PizzaOrderException e)
If something goes wrong:
catch (PizzaOrderException e)Catches your custom exception
Prevents the app from crashing
Show error message
Notification.show(e.getMessage());Displays the error text from the exception
Example message:
The min. price is 8.0 Eur
👉 User sees feedback instead of a broken UI
Dialog
ConfirmDialog dialog = new ConfirmDialog();
dialog.setHeader("Error");
dialog.setText(e.getMessage());
dialog.setConfirmText("OK");
dialog.open();