Attempted suicide was a prosecutable offense in the UK until 1961, and attempted suicide is still illegal in many places. Why is this the case? What end is hoped to be achieved by imprisoning someone who attempts to take their own life?
Old suicide legislation was often religiously-motivated. If religious teachings prohibit suicide, the state and/or church would often enact laws prohibiting suicide, and prescribing punishment for suicide and attempted suicide. Yes, there were (and in some places in the world, still are) legal punishments for (successful) suicide. Punishments included burial outside a cemetery, disposal of the body in a garbage heap, burial at a crossroads, decapitation before burial, cutting leg tendons before burial, staking during burial (the last three are also related to preventing return as undead), confiscation of all property (the state or ruler gets it, rather than the relatives who would normally inherit), fines (to be paid by the living relatives).
In the West, legal punishments for suicide were imposed by the Emperor Tiberius in the 1st century AD. Prior to this, suicide had been widely used in Rome to avoid punishment for crimes, in particular to avoid that part of the punishment that would affect the family: confiscation of one's possessions, which usually accompanied a death sentence or permanent exile. The news laws allowed confiscation if the person was "brought to this act by a moral awareness of having perpetrated a crime" (ob con-scientiam criminis), if they had been accused of a crime or caught in the act, and the crime was serious enough (Bosman, 2018).
Western Christian theology was considering suicide a sin, a breaking of the 6th commandment, by St Augustine, of not earlier. This became official Church doctrine in the 6th century; the 6th century also saw laws prohibiting Christian funerals for suicides. By the 13th century, suicide was being punished by confiscation of belongings.
The above were the two main motives for prohibiting suicide: religious opinion on suicide, and stopping suicide from being used to evade punishment for crimes.
Bosman (2018) notes that Amsterdam in the 16th century was in the forefront of relaxing anti-suicide legislation. Legal punishment for suicide (such as no Christian burial) was not always applied (e.g., one case where the person showed great remorse between inflicting their fatal wound and later dying). Where suicide was used to avoid punishment for some other crime, the body was often punished post-mortem - e.g., by being hung and exposed, as the accused would have been if they had been tried and found guilty of their crime. This was the beginning of a trend away from such laws and punishments, which accelerated greatly over the last century. Partly, this has been due to a general secularisation of law, and partly due to intent to help those who attempt suicide (if attempted suicide is illegal, does this deter those who attempt suicide from seeking medical care?).
The legality or illegality of suicide and attempted suicide can have important consequences. This last point above - criminalisation of attempted suicide can affect health care - is one such way. If suicide or attempting suicide are crimes, then generally incitement to commit suicide, aiding and abetting suicide, conspiracy to commit suicide, etc., are crimes under laws on incitement, aiding and abetting, etc. Thus, the English Suicide Act of 1961 which decriminalised suicide also made assisting suicide illegal. Insurance can be affected: if a life insurance policy is void if the person dies while committing a crime, the insurer need not pay even if the policy does not specifically exclude suicide.
References
Bosman, Machiel. "1. The Judicial Treatment of Suicide in Amsterdam". From Sin to Insanity, edited by Jeffrey Watt, Ithaca, NY: Cornell University Press, 2018, pp. 9-24. https://doi.org/10.7591/9781501732614-004