Proper way of .Net timezone validation

Viewed 2049

I have a method that accepts .Net (or windows) timezone id as a string:

public void SaveTimezoneId(string timezoneId) { ... }

As you may know, timezones in .Net are strings like this:

  • Arabic Standard Time
  • Russian Standard Time
  • Canada Central Standard Time

etc.

Before saving them to database, I need a way to validate them. What I do now looks like this:

    try
    {
        TimeZoneInfo.FindSystemTimeZoneById(timezoneId);

        CurrentUser.TimezoneId = timezoneId;
        context.SaveChanges();

        return JsonSuccess();
    }
    catch(Exception)
    {
        return JsonError("Invalid time zone");
    }

But it completely breaks one of the main principles of good software development: never rely on exceptions. And moreover, exceptions are expensive.

Is there any other way to perform this check and get rid of try-catch?

Thanks.

1 Answers
Related