I have an exception :
class WrongHourException implements Exception {}
and I have a function that check many things such as checking hour :
void confrimSchedule({
required String startHour,
required String startMinute,
required String endHour,
required String endMinute,
required String periodMinute,
required String day,
}) async {
late double start;
late double end;
late double period;
//check the validation of hours
if (int.parse(startHour) > 24 || int.parse(startHour) < 0) {
throw WrongHourException();
} else if (int.parse(endHour) > 24 || int.parse(endHour) < 0) {
throw WrongHourException();
} else if (int.parse(endHour) < int.parse(startHour)) {
throw WrongHourException();
}
//check the validation of start minute
if (startMinute == "" || startMinute == "00" || startMinute == "0") {
start = double.parse(startHour);
} else if (startMinute == "15") {
start = double.parse("$startHour.25");
} else if (startMinute == "30") {
start = double.parse("$startHour.5");
} else if (startMinute == "45") {
start = double.parse("$startHour.75");
} else {
throw WrongeMinuteException();
}
//check the validation of end minute
if (endMinute == "" || endMinute == "00" || endMinute == "0") {
end = double.parse(endHour);
} else if (endMinute == "15") {
end = double.parse("$endHour.25");
} else if (endMinute == "30") {
end = double.parse("$endHour.5");
} else if (endMinute == "45") {
end = double.parse("$endHour.75");
} else {
throw WrongeMinuteException();
}
//check the validation of preoid of each turn
if (periodMinute == "30") {
period = 0.5;
} else if (periodMinute == "45") {
period = 0.75;
} else if (periodMinute == "60") {
period = 1;
} else if (periodMinute == "90") {
period = 1.5;
} else {
throw WrongePeriodException();
}
//generate new schedule
List newTurns = generateTurns(start: start, end: end, period: period);
//update new schedule in cache
TurnData().turns[day]!.clear();
for (String time in newTurns) {
TurnData().turns[day]!.add(
Turn(
time: time,
isAvailable: false,
name: "",
number: "0",
),
);
}
//update database
await ServerService.updateDatabase(attr: day, value: [
DateData().idate[day]!.showDate(),
TurnData().dataBaseStructure(day),
]);
}
this is where I want to use try/catch :
// update scheduel
on<ScheduelEventUpdateScheduel>((event, emit) async {
try {
if (haveNumber(event.day)) {
bool isSure = await showAlertDialog(
context: event.context,
content: "با تغییر ساعت کاری نوبت ها لغو می شوند. آیا مطمئن هستید؟",
);
if (isSure) {
emit(ScheduelStateDayView(
day: event.day,
isLoading: false,
isScheduelLoading: true,
));
sendCancleNotifications(
day: event.day,
context: event.context,
dayP: event.dayP,
);
confrimSchedule(
startHour: event.startHour,
startMinute: event.startMinute,
endHour: event.endHour,
endMinute: event.endMinute,
periodMinute: event.period,
day: event.day,
);
emit(ScheduelStateDayView(
day: event.day,
isLoading: false,
isScheduelLoading: false,
));
}
} else {
bool isSure = await showAlertDialog(
context: event.context,
content: "آیا می خواهید ساعت کاری خود را تغییر دهید؟",
);
if (isSure) {
emit(ScheduelStateDayView(
day: event.day,
isLoading: false,
isScheduelLoading: true,
));
confrimSchedule(
startHour: event.startHour,
startMinute: event.startMinute,
endHour: event.endHour,
endMinute: event.endMinute,
periodMinute: event.period,
day: event.day,
);
emit(ScheduelStateDayView(
day: event.day,
isLoading: false,
isScheduelLoading: false,
));
}
}
} on Exception catch (e) {
emit(ScheduelStateDayView(
day: event.day,
isLoading: false,
isScheduelLoading: false,
exception: e,
));
}
});
I am using async/await on some other function not confrimScheduel(in scheduel_bloc) or checking validation of hour(in scheduel_functions).
but I got an Error that telling me Unhandled Exception : Instance of 'WrongHourException'
why try/catch doesn’t working?
2
Answers
When you call
You are not waiting for the result. Change that line for
or
let me say an example :
in this situation you can’t handle error but if you change your code like this:
you’ll be able to handle your exception.
so you have to change
void confrimScheduel
toFuture<void> confrimScheduel