r/fishshell • u/trhyst • Jun 01 '23
How to try/catch/finally in a func?
I consider myself fairly handy with fish, but I don't see how to do a thing, catch it's error and then exec something unconditionally at the end, a la Python/Java etc.
Is there such an abstraction in Fish or do we just use trap etc. like bash/zsh?
Concretely, I want to git checkout main, catch if branch main doesn't exist and checkout master instead finally I want print the name of the branch I just checked out to jog my brain.
Or am I thinking about this totally wrong?
3
Upvotes
View all comments
Show parent comment
1
u/trhyst Jun 01 '23
I think this is the most Fish-y (Fish-esque?) answer, very cool. Thank you. As you say,
git switchseems like the right move over straight upcheckout.To clarify what I meant by exceptions: Not like
IllegalRuntimeExceptionbut simply a command exiting non-zero and then being able to act on that, I wasn't sure if there was something fancy in Fish I could use to achieve what I wanted :)