Catalonia tried to secede during the War of the Spanish Succession but was defeated.
Catalonia didn't try to secede during the War of the Spanish Succession. They did try to secede, and did so for a few years, in 1640 when they revolted during the thirty years war. During the War of the Spanish Succession, Catalonia (actually the whole Aragon) supported the Hapsburg candidate against the Bourbon candidate favored by Castile. Barcelona was the one of last city to fall (Palma fell later still), and that's why the Catalonian nationalist movement make that a symbol of the struggle for independence. The truth, however, is that the town of Barcelona called ALL SPANIARDS to fight agains the Bourbons, never claiming for independence.
The war of Spanish succession falling into stalemate and ending up with Spain split between Bourbon Aragon and Hapsburg Castille-Leon would be an interesting way to do it.
If the war of the succession had fallen into stalemate it would have ended up with a Bourbon Castile (centralized) and a Hapsburg Aragon. However I don't think either of the two candidates had agreed to that.
During the middle ages, all the different kingdoms in Iberia had the idea of getting united. After all, they all claimed to be spaniards and called themselves spaniards, descendants from the visigothic kingdom. Even Ferdinand and Isabella had that idea after their marriage, and eventually it became a reality in 1580 when Philip II inherited the crown of Portugal.
However, a union between Castile and Portugal would have made much more sense. If Henry IV, Isabella's brother, had had more power over her he could have made her marry Juan of Portugal and eventually Castile and Portugal would have united. Aragon, back then, was suffering a severe decline, and without Castile's help all their possesions in Italy would have been lost. I have the feeling that the next step for France would have been invading Aragon through Catalonia, and even though Castile and France had been traditionally allies until then, probably Castile would have reacted entering the war on the side of Aragon.
Then again, what Gonzaga says is true: if only Ferdinand had had a heir from his second marriage the personal union would have been broken and Aragon would have remained independent. The question is, for how long?