Alan Dixon, a former consultant physician specialising in rheumatology, told Winchester Crown Court he hoped he would have taken the same action as the defendant in similar circumstances.
Nigel Cox, 47, consultant rheumatologist at the Royal Hampshire County Hospital in Winchester, denies attempting to murder Lillian Boyes, 70, in August last year by giving her a lethal dose of potassium chloride.
Mrs Boyes, a widow with acute rheumatoid arthritis and severe complications, died within minutes of the injection. She had been refusing treatment and had asked Dr Cox to help her die.
The court heard from two medical experts that she was close to death and in agonising pain despite being given heroin in such massive doses that it could have shortened her life, a practice regarded as medically proper.
Asked by Sydney Kentridge QC, for the defence, if he would have taken similar action to Dr Cox, Dr Dixon replied: 'I hope I would have had the courage to do so. I would have been ashamed of myself if I hadn't'
Under cross-examination by Neil Butterfield QC, for the prosecution, he said: 'The law leaves us in a very curious condition. We are allowed to give drugs to relieve pain even if they shorten life and at the same time we are not allowed to give drugs to shorten life even if they relieve pain.
'That's the sort of predicament we are in. It's a razor's edge.' Asked what further action could have been taken to control Mrs Boyes's pain, he said: 'Dr Cox had the option between walking away and doing what he did.'
David Blake, professor of rheumatology at the Royal London Hospital, told the court that Dr Cox, who he said had an 'exceptional reputation' in his field, had faced 'a terrible dilemma' because, most unusually, the heroin was not working and there was no stronger painkiller.
'In these extreme circumstances, you could feel sympathy for a doctor who walked away,' he said.
Asked what Dr Cox could have done, Professor Blake replied: 'One of his options was to do nothing more than give her a cuddle, but the consequence of that would have been to drop her blood pressure so low that she would have died within seconds.'
Professor Blake said that Mrs Boyes had been suffering from the worst case of rheumatoid arthritis of which he had heard. He agreed that it was proper to administer painkillers that could hasten a patient's death, saying: 'There are worse things than death.'
Mr Kentridge told the jury that the single issue that they had to consider was whether Dr Cox's primary intention when he injected Mrs Boyes with potassium chloride was to relieve her suffering or to kill her.
Although the deliberate shortening of life was unlawful, 'there are some situations where the law does not take such an absolute position . . . where the law is tempered with a very necessary humanity and common sense'.
Mr Kentridge said that when a patient was close to death, 'a doctor . . . has the right and indeed the duty to take all measures possible to relieve . . . suffering even though he knows that it will hasten that patient's death'.