skip to Main Content

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?

this is error image

2

Answers


  1. When you call

    confrimSchedule( // line 161 scheduel_bloc.dart
              startHour: event.startHour,
              startMinute: event.startMinute,
              endHour: event.endHour,
              endMinute: event.endMinute,
              periodMinute: event.period,
              day: event.day,
            );
    

    You are not waiting for the result. Change that line for

    await confrimSchedule( // line 161 scheduel_bloc.dart
              startHour: event.startHour,
              startMinute: event.startMinute,
              endHour: event.endHour,
              endMinute: event.endMinute,
              periodMinute: event.period,
              day: event.day,
            );
    

    or

    confrimSchedule( // line 161 scheduel_bloc.dart
              startHour: event.startHour,
              startMinute: event.startMinute,
              endHour: event.endHour,
              endMinute: event.endMinute,
              periodMinute: event.period,
              day: event.day,
            ).then((_){
    //emit here
    });
    
    Login or Signup to reply.
  2. let me say an example :

    class WrongHour implements Exception {}
    
    void checkHour(int hour) async {
      await Future.delayed(const Duration(seconds: 1));
      if (hour > 24 || hour < 0) {
        throw WrongHour();
      } else {
        print("good to go");
      }
    }
    
    void main() {
      try {
        checkHour(25);
      } on Exception catch (e) {
        print(e);
      }
    }
    

    in this situation you can’t handle error but if you change your code like this:

    class WrongHour implements Exception {}
    
    Future<void> checkHour(int hour) async {
      await Future.delayed(const Duration(seconds: 1));
      if (hour > 24 || hour < 0) {
        throw WrongHour();
      } else {
        print("good to go");
      }
    }
    
    void main() async {
      try {
        await checkHour(25);
      } on Exception catch (e) {
        print(e);
      }
    }
    

    you’ll be able to handle your exception.
    so you have to change
    void confrimScheduel to Future<void> confrimScheduel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search