Sen. Bernie Sanders and former secretary of state Hillary Clinton sparred over fiscal issues, national security and guns on Saturday night as the final televised Democratic presidential debate of the year exposed some sharp differences between the party’s two leading candidates.

Sanders (Vt.) cast Clinton as too hawkish, arguing that she is advocating overly aggressive strategies in the Middle East. Clinton chided Sanders for not embracing stronger gun control laws earlier in his career. And she slammed his single-payer health-care plan, arguing its creation would impose steep costs.

Both candidates appeared eager to turn the page on the biggest development coming into the debate: A spat over Clinton campaign voter data that Sanders staffers improperly viewed after a glitch in a Democratic National Committee database. Sanders apologized and Clinton quickly accepted.

Former Maryland governor Martin O’Malley, a long-shot hopeful, went on offense against the two front-runners, casting himself as a new generation candidate with a more proven and liberal record than his opponents. But for the most part, the back-and-forth between Clinton and Sanders dominated the evening.

The three candidates all spoke at length about reforms to combat heroin addiction, including curbing the over-prescribing of opiates, changing law enforcement responses and setting aside more government funding to deal with the crisis.

At the third Democratic presidential debate, Sen. Bernie Sanders (I-Vt.) apologized to rival Hillary Clinton, grew heated about gun control and urged America to focus on defeating the Islamic State. Here are some of his most heated moments. (Sarah Parnass/The Washington Post)

“This is a major epidemic,” said Clinton.

At one point, Clinton took aim at Sanders’s health care and education proposals. “You’re going to have to get more taxes out of middle-class families,” Clinton told Sanders of his proposal for a single-payer health-care system.

Sanders has called for free college tuition and public colleges and universities. Clinton disagreed, arguing in favor of tuition relief for “middle class families and poor kids.”

Sanders quickly disputed the Democratic front-runner’s contention.

“The middle class will be paying substantially less for health care under single-payer,” he said.

Clinton was the only candidate who vowed during the debate not to raise taxes on American families making under $200,000 a year. “That is a pledge that I’m making,’ she said.

Which candidate fits you?

As the debate started to focus more heavily on fiscal issues, Sanders, who prefers to talk about income inequality over all else, appeared to settle in.

“Now this is getting to be fun,” he quipped.

Earlier, Sanders and Clinton offered different responses to the question of whether corporate America would approve of them as president.

“Should corporate America love Hillary Clinton?” asked ABC News moderator David Muir.

“Everybody should!” Clinton responded, arguing that she is invested in helping everyday and “struggling” Americans improve their economic standing by also working with large companies to accomplish that goal.

Sanders had a very different take.

“No, I think they won’t,” he said flatly when asked whether corporate America would like a President Sanders.

Late in the debate, Clinton was pressed over the Obama administration’s handling of Libya when she was secretary of state.

“I’m not giving up in Libya. I don’t think anybody should,”said Clinton.

“Were mistakes made?” co-moderator Martha Raddatz asked her.

“There’s always a retrospective to say what mistakes were made,” Clinton responded.

Sanders jumped in to call Libya “a terribly complicated issue.” As he did earlier in the debate, Sanders argued that Clinton is too invested in regime change.

“I think Secretary Clinton and I have a fundamental disagreement,” he said.

Sanders tried to draw a clear distinction with Clinton on national security, arguing that she is “too aggressive.”

He also emphasized that defeating the Islamic State is a more pressing national security priority in the Middle East than ousting Syrian President Bashar al-Assad. Clinton argued the two can and should happen concurrently.

“I think we’re missing the point here,” said Clinton. “We are doing both at the same time.”

Sanders argued that “it is not Assad who is attacking the United States. It is ISIS.”

Both Clinton and Sanders cautioned strongly against sending ground troops en masse to Iraq and Syria to fight the Islamic State at the final Democratic presidential debate of the year.

“I think it’s absolutely the wrong policy for us to be even imagining that we are going to end up putting tens of thousands of troops into Syria and Iraq to fight ISIS,” Clinton said.

Clinton voiced support for President Obama’s decision to dispatch special operations forces to the region and warned that a large U.S. ground force would play into the enemy’s hands.

“They want American soldiers on the ground fighting them,” she argued.

Sanders expressed a similar sentiment.

“The United States of America cannot succeed or be thought of as the policeman of the world,” he said.

Earlier, the Democratic presidential candidates engaged in a fiery debate over gun control, lobbing barbs at one another as they pitched themselves as staunch advocates of tightening gun laws.

“Arming more people to do what ... I think is not the appropriate response to terrorism,” Clinton said, in a discussion that also involved national security.

O’Malley went hard after both Clinton and Sanders, accusing them of not being consistent advocates of gun control.

“Excuse me,” Sanders responded forcefully to O’Malley. “Do not tell me that I have not shown courage in standing up to the gun people.”

Clinton went after Sanders for his past record on guns, which has concerned some gun control advocates. She said she was pleased to see he has “moved” in face of the facts.

The exchange stood in sharp contrast to the Republican debates, in which there has been virtually no talk about tightening gun laws on the wake of terror attacks and other mass shootings.

After a commercial break during the debate, there was an awkward moment when Clinton had not returned to her podium but Sanders and O’Malley were there. The moderators decided to continue the questioning anyway.

Shortly after, Clinton returned to the stage to applause.

“Sorry,” she said, without an explanation.

Clinton, Sanders, and O’Malley took the stage at Saint Anselm College in Manchester, N.H., at 8:30 p.m. ABC News and the New Hampshire Union Leader are sponsoring the Democratic debate, which is the third of the campaign.

At the beginning of the debate, Sanders apologized to Clinton Saturday night improperly accessing voter information gathered by Clinton’s campaign after a computer glitch in a database managed by the Democratic National Committee.

“I apologize,” Sanders told Clinton at the final Democratic debate of the year. He also apologized to his supporters.

Clinton, whose aides have slammed Sanders over the incident, appeared eager to turn the page.

“We should move on because I don’t think the American people are all that interested in this,” she said, after telling Sanders, “I very much appreciate that comment.”

The spat was set off this week when Sanders staffers accessed information gathered by Clinton’s campaign after a computer glitch in a database managed by the DNC, drawing criticism from Clinton aides. The incident added an unexpected twist to the prime-time set-to, which had been poised to focus on foreign policy and national security.

As the debate wrapped up Saturday, the candidates were asked about the changing role of presidents’ spouses.

Clinton said of her husband, former President Bill Clinton, “I am probably still going to pick the flowers and the china for state dinners and stuff like that, but will certainly turn to him, as prior presidents have” for “special missions,” the economy and other issues.

The debate came as Clinton holds a wide national lead over Sanders. A Washington Post-ABC News poll released this week showed Clinton ahead of Sanders, 59 percent to 28 percent, on par with her standing a month ago. O’Malley lagged behind at just 5 percent.

The contest in the early states is closer, although Clinton has bolstered her standing in those places lately. Clinton led Sanders 48 percent to 39 percent in a recent Iowa poll conducted for the Des Moines Register and Bloomberg Politics. A Franklin Pierce University and Boston Herald poll of New Hampshire showed them running neck-and-neck.

The race took a sharp detour Thursday when DNC officials accused the Sanders campaign of improperly accessing confidential voter information gathered by Clinton’s team. Sanders campaign manager Jeff Weaver said a staffer was fired over the matter, which he blamed on a software error caused by a vendor hired by the DNC.

On Friday, tensions escalated. Weaver accused the DNC of trying to help Clinton, and the Sanders campaign filed a lawsuit against the DNC for suspending its access to key voter information after the incident. Early Saturday, the DNC and Sanders issued statements saying that his access had been restored, though each side used tough language to talk about the other.

Meanwhile, Clinton aides expressed deep concerns about the Sanders campaign accessing the information.

“This was a very egregious breach, and our data was stolen. This was not an inadvertent glimpse into our data,” said Clinton campaign manager Robby Mook.

After the Sanders campaign and the DNC appeared to come to terms on an arrangement, Clinton press secretary Brian Fallon issued a gentler statement, saying, “We are pleased that the Sanders campaign has agreed to submit to an independent audit to determine the full extent of the intrusion its staff carried out earlier this week.”

At issue is a master list of voter data that is key in targeting and fundraising efforts. The DNC rents it out to campaigns, which add their own proprietary information. The vendor that maintains the list said a computer error on Wednesday briefly allowed the campaigns to review information they would not normally be able to see.

Sanders, who is running as a Democratic socialist laser-focused on income inequality, is trying to make up ground in the race against Clinton ahead of the first nominating contest in Iowa on Feb. 1.

Clinton looked vulnerable earlier this year amid an uproar over her use of a personal email account during her time as the nation’s chief diplomat. But in recent weeks, she has steadied her campaign, emphasizing her positions on national security and running hard against the Republican field on national security.

Sanders on Thursday won the endorsement of the 700,000-member Communications Workers of America, a rare blow to Clinton’s powerful army of organized labor supporters.

John Wagner, Abby Phillip, Rosalind S. Helderman and Anne Gearan contributed to this report.