Answer the question
In order to leave comments, you need to log in
What to use throw + try/catch or if + return?
Good day to all!
There is a phonebook application in JavaFX. It consists of two forms (Stage).
On the first table and three buttons: add, change, delete. The table stores objects of type Person with two fields: fio and phone. The second is for entering or changing data from the table, contains two buttons "Save" and "Cancel".
Forms: mainPane.fxml, addPane.fxml. Two controllers (classes with event handlers) to them: MainPaneController.java and AddPaneController.java respectively.
The controller for the main window looks like this:
public class MainPaneController {
....
private AddPaneController addPaneController;
//Коллекция объектов Person - записей телефонной книги
private CollectionAddressBook addressBookImpl = new CollectionAddressBook();
...
@FXML
private void initialize(){
...
addPaneController = fxmlLoader.getController();
addressBookImpl.fillPersonList();
}
...
@FXML
private void actionButtonPressed(ActionEvent actionEvent){
Object source = actionEvent.getSource();
if (!(source instanceof Button)) return;
Button clickedButton = (Button) source;
try {
switch (clickedButton.getId()){
case "btnAdd":
addPaneController.setPerson(new Person());
showDialog(TypeDialog.ADD);
addressBookImpl.add(addPaneController.getPerson());
break;
case "btnEdit":
Person selectedPerson = (Person)tableAddresBook.getSelectionModel().getSelectedItem();
addPaneController.setPerson(selectedPerson);
showDialog(TypeDialog.UPDATE);
break;
case "btnDelete": break;
}
}
catch (EmptyPersonException e){}
catch (NullPersonException e){
DialogManager.showErrorDialog(e.getMessage());
}
}
...
private void showDialog(TypeDialog typeDialog) {
// Отображает форму addPane.fxml
}
}
public class AddPaneController {
private Person person;
@FXML
private TextField txtFIO, txtPhone;
public void setPerson(Person person) throws NullPersonException {
if (person == null)
throw new NullPersonException();
this.person = person;
txtFIO.setText(person.getFio());
txtPhone.setText(person.getPhone());
}
public Person getPerson(){
return person;
}
//region LISTENERS
//для кнопки "Отмена"
@FXML
public void actionClose(ActionEvent actionEvent){
Node source = (Node)actionEvent.getSource();
Stage stage = (Stage)source.getScene().getWindow();
stage.hide();
}
//для кнопки "Сохранить"
@FXML
public void actionSave(ActionEvent actionEvent) {
try {
person.setFio(txtFIO.getText());
person.setPhone(txtPhone.getText());
actionClose(actionEvent);
}
catch (EmptyPersonException e){
DialogManager.showErrorDialog(e.getMessage());
}
}
//endregion
}
public class Person {
private SimpleStringProperty fio = new SimpleStringProperty("");
private SimpleStringProperty phone = new SimpleStringProperty("");
public Person(){}
public Person(String fio, String phone) {
this.fio.set(fio);
this.phone.set(phone);
}
public String getFio() {
return fio.get();
}
public String getPhone() { return phone.get(); }
public void setFio(String fio) throws EmptyPersonException {
if (fio.length() == 0)
throw new EmptyPersonException();
this.fio.set(fio);
}
public void setPhone(String phone) throws EmptyPersonException{
if (phone.length() == 0)
throw new EmptyPersonException();
this.phone.set(phone);
}
}
public class CollectionAddressBook implements IAddressBook {
private ObservableList<Person> personList = FXCollections.observableArrayList();
@Override
public void add(Person person) throws EmptyPersonException {
if (person.getFio().length() == 0 || person.getPhone().length() == 0)
throw new EmptyPersonException();
personList.add(person);
}
...
public ObservableList<Person> getPersonList(){ return personList;}
public void fillPersonList(){
personList.add(new Person("Артем","8 800 555 3535"));
personList.add(new Person("Василий","8 802121 3535"));
personList.add(new Person("Нина","8 23222390 3535"));
}
}
public class EmptyPersonException extends Exception {
public EmptyPersonException(){
super("Не все поля заполненны!");
}
}
public class NullPersonException extends Exception {
public NullPersonException(){
super("Не выбрана запись из списка!");
}
}
@FXML
private void actionButtonPressed(ActionEvent actionEvent){
Object source = actionEvent.getSource();
if (!(source instanceof Button)) return;
Button clickedButton = (Button) source;
switch (clickedButton.getId()){
case "btnAdd":
addPaneController.setPerson(new Person());
showDialog(TypeDialog.ADD);
if (person.getFio().length() == 0 || person.getPhone().length() == 0){
DialogManager.showErrorDialog("Не все поля заполненны!");
return;
}
else
addressBookImpl.add(addPaneController.getPerson());
break;
case "btnEdit":
Person selectedPerson = (Person)tableAddresBook.getSelectionModel().getSelectedItem();
if (selectedPerson == null){
DialogManager.showErrorDialog("Не выбрана запись из списка!");
return;
}
else{
addPaneController.setPerson(selectedPerson);
showDialog(TypeDialog.UPDATE);
}
break;
case "btnDelete": break;
}
}
Answer the question
In order to leave comments, you need to log in
The difference between if / else is that it needs to be set every time after each operation at each stage in order to check.
And you hang try / catch on the entire block, and you immediately catch different events.
If performance is important to you then if/else is faster
It's up to you to decide :)
1) try/catch - it is not recommended to use it in a loop, as this design reduces performance.
2) if/else - much faster
You don't have loops for 1_000_000 iterations in which try/catch is used, which means that it will be imperceptible for your application personally.
Whatever is more convenient for you personally as a developer, then use it.
Question: how usable in this situation (for checking the fields of the Person object) is the use of the exception mechanism?
Maybe it was worth using if and return, for example?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question