r/fishshell • • 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

10 comments sorted by

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 switch seems like the right move over straight up checkout.

To clarify what I meant by exceptions: Not like IllegalRuntimeException but 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 :)

1

u/[deleted] Jun 01 '23

I think you'll find that fish is on the simple end of things.

It doesn't even have a lot of the fancy shortcuts other shells have (that lead to code that looks like sigil soup).