Hey,
I think it’s possible, but it’s not simple. You can check out this post for somewhat of an answer: A script to uncheck "Exit Time" on an animation while button is held down.
If you want a really simple solution, you can check out Animancer: https://assetstore.unity.com/packages/tools/animation/animancer-lite-116516
It allows you to easily choose when an animation ends through code. You can try it out for free, but the pro version is paid. I’ve been using it for a while and it solves a lot of the problems I was facing with mecanim, so I think it’s worth it, but your mileage may vary.
Best of luck!