Getting canned can advance your career – if handled correctly.

Take Preet Bharara, who stood his ground after the Trump Administration asked 46 U.S. Attorneys to resign on Friday. The Southern District of New York lawyer took his surprise dismissal to Twitter.

“I didn’t resign. Moments ago I was fired,” he wrote in a viral post that has received more than 133,000 likes, been retweeted 64,000 times and made him a household name. “Being the US Attorney in SDNY will forever be the greatest honor of my professional life.”  

His tweet has dominated headlines, drawn support from Sen. Chuck Schumer and “Billions” creator Brian Koppleman, and rocked the White House. Bharara is already scheduled to lecture at Cooper Union on April 6. And while he has dismissed future political aspirations or private practice, both are on the table.

Sree Sreenivasan, the former Chief Information Officer of the Metropolitan Museum of Art, also took the high road on social media when he was let go last summer.

“It’s time for Sree 3.0. And I want your input,” wrote Sreenivasan in a Facebook post that diplomatically addressed the bad news, but still thanked his former employer while informing potential new ones about what he was working on. “If you want to invite me to anything, I now have time, including for meaningful cups of coffee and drinks,” he added.

It worked. He’s now the CIO of New York City.

Here’s how you can turn your own firing into a new career:

Don’t trash talk. Even if you’re surprised or hurt about a sudden pink slip, don’t trash your former employer or coworkers online or offline. Keep it civil.

Reframe a bad outcome. Turn this setback into an opportunity to make a career change or re-brand yourself in the job market – especially if you’re in a struggling field. “For example, some people in the real estate market decided during the recession to apply their skills to new industries because it would be some time before the housing market recovered,” said job coach Cheryl Palmer, owner of Call to Career.  Your resume should indicate how the skills you learned in your former career apply to the new one; for example, a real estate professional may have plenty of sales skills, which could apply in many fields. 

Embrace charity work. Paying it forward by donating your newfound free time can have big payoffs. Besides filling in that awkward employment gap on your resume, volunteering opens up networking opportunities that can lead to future jobs. You can also hone leadership and team-building skills by organizing fundraisers, calling in donations and recruiting other volunteers.

Use this time to try your hand at a passion project. It may pay off.  You may not immediately find a job, but that doesn’t mean you won’t.  Being let go can free you to go in a direction that is a better fit for you,” said Palmer — it may give people “the motivation that they need to pursue their passions.” Like J.K. Rowling, who lived on her severance check after getting axed as a secretary for Amnesty International to finally focus on a dream she had shelved for years: Writing about a boy wizard named Harry Potter.

Have a concise explanation ready for your firing. Once you begin job hunting, come up with a short, straightforward and positive explanation for why you left your last job. Don’t spend more than 10 or 20 seconds addressing it, and don’t lead your resume or cover letter with the news that you were let go, suggest the career experts at Monster.comMake it clear to interviewers that the reason you were cut isn’t about you, such as explaining that your entire department was downsized, or – as in Bharara’s case – that your new boss wanted to bring in his or her own team. Employers recognize that this happens all the time.