The answer in the case of criminal charges in the federal system and in the vast majority of U.S. states is that you can almost never recover legal fees you incur defending a criminal action.
There is such a thing as a lawsuit for malicious prosecution, and there is such as thing as a lawsuit for a civil rights violation caused by bringing baseless charges, but in both circumstances one must demonstrate that the charges were brought without probable cause.
But, in most cases of serious criminal charges like this one, either a grand jury probable cause finding, or a preliminary hearing probable cause finding, both made well prior to a trial, will conclusively preclude a malicious prosecution or civil rights lawsuit. (Also, prosecutors have absolute immunity for their discretionary prosecution decisions, and judges have absolute immunity for their judicial decisions, and you can't sue jurors unless they accepted a bribe or something like that.) But, the standard of proof necessary to establish probable cause is much lower than the standard of proof necessary to convict.
A very small minority of states allow for reimbursement of fees upon an acquittal, but even then, it is often necessary to prove by a preponderance of the evidence that you were actually innocent, so a dismissal on procedural grounds or an acquittal at a criminal trial where the prosecution must show beyond a reasonable doubt that you are guilty, is not sufficient to show that it is more likely than not that you are innocent. Likewise, you are not entitled to recovery for indirect financial damages caused by criminal charges.
As you correctly imply, this is a very harsh rule that can mean that wrongful criminal charges can ruin you.
On the other hand, if you are unable to afford an attorney, and a public defender is appointed for you by the state, you do not have to reimburse the state for the public defender's fees if you prevail and are acquitted. A minority of states, however, require that you reimburse the state for the public defender's fees if you are convicted along with other court costs, fines and restitution awards.
For what it is worth, only about 1% of criminal charges brought result in an acquittal at trial. Most cases are resolved through a pre-trial plea bargain, a voluntary dismissal by a prosecutor who acknowledges that there is no case against you prior to trial, or a conviction of at least something at trial. About 10% of cases go to trial and about 10% of cases that go to trial result in an acquittal or hung jury (in very round and approximate numbers that vary greatly from jurisdiction to jurisdiction and by type of case). Also, probably at least 10% of acquittals are of people who were factually guilty, because juries get it right something on the order of 90% of the time when cases go to trial.
But, the vast majority of acquittals result in a vast injustice to the defendant (although not as great as when a judge uses the factual basis of the events from which you have been acquitted to enhance the sentence against you on other charges which is done from time to time in both the federal and state legal systems in a practice that is unfair but not necessarily sufficient to overrule those sentences on appeal).
The situation in a civil case is different and too broad to answer in one question. There are some civil cases where a prevailing defendant is entitled to attorneys' fees and costs, while there are others where a prevailing defendant is not. The default rule, called the "American Rule" is that a prevailing attorney is not entitled to attorneys' fees and costs of a defense.
But, there are myriad exceptions to that rule that vary by type of case, by the particular details of how a case was prosecuted, and by legal jurisdiction within the United States, that are not easily summarized. For example, in Colorado civil cases, some of the more common grounds for an award of attorneys' fees to a defending party are: (a) a two-sided contractual fee shifting term, (b) dismissal of the case before filing an answer for failure to state a claim when tort claims were asserted, (c) a determination that the suit was groundless, frivolous or vexatious, (d) violation of certain rules relating to disclosure of information to the other party, (e) a statutory fee shifting provision in the case of a claim based upon a statutorily created right which is present in some statutes but not others.