There are four possiblities.
A. No dynasty, no heir
B. No dynasty, weak heir / regency
C. Same dynasty, no heir
D. Same dynasty, weak heir / regency
I'll elucidate:
A : You do not share a dynasty. They have no heir. You may claim the throne.
If they die without an heir, you get a personal union. If they produce an heir, your claim is removed. You may not start a war to create a union.
B : You do not share a dynasty. They have a weak heir. You may claim the throne. This is pointless and you will get nothing.
C : You share a dynasty. They have no heir. You may claim the throne. If they die without an heir, you get a personal union.
If they produce an heir, your claim is removed. You receive casus belli to start a war, and may force a union. If they produce an heir before you begin that war, you lose the casus belli and the claim to the throne.
D : You share a dynasty. They have a weak heir or a regency. You may claim the throne. You receive casus belli to start a war, and may force a union.
If the regency ends or the king dies and the heir ascends, you will lose the casus belli and the claim to the throne.
E : You, the target country, and a 3rd country share a dynasty. They have no heir. You may claim the throne. If you claim the throne and they die without an heir, you get a personal union.
If the 3rd country also claims the throne, a succession war begins. If they die without an heir and neither of you claim the throne, a succession war begins.
If you claim the throne, you may begin a war to force a union. If they produce an heir, all claims are removed.
F : You share a dynasty. I don't know the conditions, but you inherit the throne, bypassing a personal union completely.
Note that it is possible to share a dynasty without ever having married a country yourself. A marries B, B marries C, A and C can end up sharing a dynasty. And so on.