In the case that startup I'm working on for several years is going to fail, what is the best way to get most experience out of it? I see the following options:
I'm a technical cofounder(developer) so my knowledge is relatively well transferable into other areas.
This is very objective. First of all, ask yourself about your future in the actual startup (and it is a question if "several years" old company is still a startup, but that's other topic...).
If you think it will fail, you have two possibilities: a) you like (working in) that startup, it worths the effort and thus you will make all you can to prevent the failure.
b) you feel that the ship is sinking and it can't be saved / it doesn't worth the effort to try to save it.
In case a), just stop thinking about the "if questions" and focus on the rescue of the startup.
In case b) - leave. Staying in some dying company without future is just a loss of time.
Now for your future - I must say that there is not a general solution. It's about your feelings. Listen to your gut, you already know the answer. You can start by answering these questions :
And if you still can't decide: toss a coin and then just do it by doing your best. (this is not a joke: the "no decision" is the worse...)
There's a very good answer already on the personal issues to consider. Let me answer the business issues.
Generally speaking, it is best to start your second business within the shell of the first business. When people ask questions like 'how long have you been in business' you can say '7 years' as opposed to 'six months'. The longevity helps, in certain circumstances, and it saves you on the legal and accounting fees of setting up again.
There is a condition to this: it is only true if you have run your business carefully and professionally throughout (and kept your legal and and accounting issues in order). If you have ex-employees, you need to have paperwork showing they have left on good terms. You need to settle all of your outstanding debts. You need to be sure that nobody has a potential claim against you for things you did poorly in the past.
There's nothing worse than taking the business to a new area, then having a previous partner from the first business - who you didn't get proper severance with - come back and sue you a couple of years later because they are annoyed with you.
So if you're not sure about the accounting and legal history of the first company (and as the tech founder, this will require a careful review of the business affairs of the company, which you didn't look after), you'll have to shut it down and start over.
P.S. I hope you've been paid some sort of salary in the past years!