While [the documentation][1] says "any valid expression," I don't think it's quite true. And unfortunately this is one of those cases where the error message about length happens before `TRY_CAST` is ever able to _gracefully_ handle your input.

Even though it is clear that `TRY_CAST` _will_ accept strings longer than 4000:

    SELECT DATALENGTH(TRY_CAST(REPLICATE('x', 4001) AS nvarchar(max)));

    ----------
    8002

Now, it is questionable why you would ever try to cast a string this long to an int in the first place, because the longest a _valid_ and _reasonable_ value could be would be ~40 characters, e.g.

    SELECT TRY_CAST(0.00000000000000000000000000000000000001 AS int);

If the goal of the code is to convert to an `int`, there's really no reason to allow more than 40 characters. 

(I guess in theory you could try to cast someone's string input of **π** as an `int`, but, we know the answer is 3, and SQL Server can't express all those decimals anyway - by default `PI()` only returns 1 decimal places.)

If you really can't control the input, then I would simply wrap the argument in `LEFT`, e.g.

    SELECT TRY_CAST(LEFT(@string, 4000) AS int);

You could use a wrapper function and all that, but I don't think you're going to find a simpler workaround. 

  [1]: https://learn.microsoft.com/en-us/sql/t-sql/functions/try-cast-transact-sql