Hitler and Goebbels amongst others committed suicide as they lost the war. Did they know they would be executed? Did prior European leaderships commit suicide on war defeat - as far as I know nobody did in Germany on the First World War loss. What made this so different?
Certainly there was an awareness among the Nazi leadership that their punishment at the hands of the victors would be severe — the Soviets (always seen by the Nazis as somehow more 'barbaric' than the Western Allies) imbued a particular terror in prominent Nazis.
For some senior Nazis — like Hermann Göring, who surrendered to the Americans and who committed suicide only after trial, the night before his execution — suicide was a final act of defiance. Göring had requested an 'honourable' military execution by firing squad; the Nuremberg judges ignored the request, ordering that he be hanged. In his suicide note, he reportedly wrote:
I would have had no objection to being shot. However, I will not facilitate execution of Germany's Reichsmarschall by hanging! For the sake of Germany, I cannot permit this.
Moreover, I feel no moral obligation to submit to my enemies' punishment.
For this reason, I have chosen to die like the great Hannibal.
For others, like Himmler, who attempted to flee to the Alps and killed himself only when he was identified and arrested by British troops, it was an act of desperation. The same is true of Martin Bormann, who was finally confirmed in 1998 as having committed suicide in Berlin in May 1945, likely to avoid capture by the Soviets.
For Joseph Goebbels, it was perhaps a last act of loyalty, following the Führer's example. Peter Longerich, in his newish biography, quotes Goebbels as adding a postscript to his will explaining why he refused Hitler's order to flee Berlin before the fall:
“For the first time in my life I must categorically refuse to obey an order of the Führer’s. My wife and children join with me in refusing to do so.” “For reasons of humanity and personal loyalty” he could “never bring himself to abandon the Führer in his greatest hour of need.” Moreover, he believed that in doing this he was “acting for the best as far as the future of the German people is concerned for in the difficult times that are approaching models to live up to will be more important than men.” He and his wife were determined not to leave Berlin “and rather to end our lives side by side with the Führer, a life which in any case is of no more value for me if I cannot live it in the Führer’s service and by his side.”
The Nazis were never blind to the fact that their actions, and the Final Solution in particular, would be viewed with utter disgust by the rest of the world if they became known; they put a great deal of effort into concealing the genocide. In the last months of the war, when total defeat became increasingly inevitable, the regime attempted (unsuccessfully) to destroy much of the documentary evidence of the Holocaust.
The Allies had made clear that they intended to seek justice (or vengeance) for the crimes of the Nazis. In October 1943, the 'Four Nations' — Britain, the US, the USSR and China — issued a series of four public declarations at the conclusion of the Moscow Conference. One of them, the Declaration on Atrocities, vowed that the consequences of Nazis crimes would be severe. Here are some key excerpts, to give you a sense of the tone of the declarations (emphasis mine):
The United Kingdom, the United States and the Soviet Union have received from many quarters evidence of atrocities, massacres and cold-blooded mass executions which are being perpetrated by the Hitlerite forces in many of the countries they have overrun and from which they are now being steadily expelled. [...]
At the time of the granting of any armistice to any Government which may be set up in Germany, those German officers and men and members of the Nazi party who have been responsible for or have taken a consenting part in the above atrocities, massacres and executions will be sent back to the countries in which their abominable deeds were done in order that they may be judged and punished according to the laws of those liberated countries and of the Free Governments which will be erected therein. [...]
[...] Thus Germans who take part in wholesale shootings of Polish officers or in the execution of French, Dutch, Belgian or Norwegian hostages or of Cretan peasants, or who have shared in the slaughters inflicted on the people of Poland or in the territories of the Soviet Union which are now being swept clear of the enemy, will know that they will be brought back to the scene of their crimes and judged on the spot by the peoples whom they have outraged.
There is precedent for military and national leaders committing or attempting suicide in the face of a defeat. Napoleon Bonaparte attempted suicide at least once, and possibly twice. He had habitually carried a vial of poison around his neck since an incident in the Russian campaign in which he had come very close to being captured by cossacks.
Shortly before he was to be exiled to Elba:
"I hesitated no longer, but leaping from my bed mixed the poison in a little water, and drank it with a sort of feeling of happiness. But time had taken away its strength; fearful pains drew forth some groans from me, they were heard, and medical assistance arrived"
(Napoleon's own recollection, quoted in Montholon's History of the Captivity at Saint Helena)
As for the possible second attempt, I will quote Adam Roberts' Napoleon the Great. Describing Napoleon's second abdication, after his defeat at Waterloo:
It is possible that Napoleon tried to poison himself again that night. Cadet de Gassicourt, the Emperor's apothecary, told General Thiebault in 1818 that he had been summoned to the Elysee on June 21 after Napoleon had swallowed poison, as he had the previous year, and changed his mind - after which the terrified Gassicourt had managed to provoke nausea and then administer fluids. Though there is no corroborating testimony, Gassicourt may well have been telling the truth.