The core of a terrorism trial must be held in secret so that justice can be done, the Court of Appeal has ruled.

Three judges found the course was justified in the "exceptional" case of Erol Incedal and Mounir Rarmoul-Bouhadjar, who are to face charges at the Old Bailey on June 16.

Lord Justice Gross, sitting with Mr Justice Simon and Mr Justice Burnett, said: "The rule of law is a priceless asset of our country and a foundation of our constitution. One aspect of the rule of law - both a hallmark and a safeguard - is open justice, which includes criminal trials being held in public and the publication of the names of defendants.

"Open justice is both a fundamental principle of the common law and a means of ensuring public confidence in our legal system; exceptions are rare and must be justified on the facts.

"Any such exceptions must be necessary and proportionate. No more than the minimum departure from open justice will be countenanced."

He said that c onsiderations of national security, which was itself a national interest of the first importance and the raison d'etre of the security and intelligence agencies, would not by themselves justify a departure from the principle of open justice.

"Open justice must, however, give way to the yet more fundamental principle that the paramount object of the court is to do justice; accordingly, where there is a serious possibility that an insistence on open justice in the national security context would frustrate the administration of justice, for example, by deterring the Crown from prosecuting a case where it otherwise should do so, a departure from open justice may be justified."

He added: "This case is exceptional. We are persuaded on the evidence before us that there is a significant risk - at the very least a serious possibility - that the administration of justice would be frustrated were the trial to be conducted in open court; for what appears to be good reason on the material we have seen the Crown might be deterred from continuing with the prosecution."

The judges were ruling on a challenge by media organisations against an "unprecedented" decision by Mr Justice Nicol that the trial should take place entirely in private with the identity of both defendants withheld and a permanent prohibition on reporting what took place during it and their identities.

They concluded that, while " as a matter of necessity", the core of the trial must be heard in camera, the swearing-in of the jury, the reading of the charges, part of the judge's introductory remarks, at least part of the prosecution opening, the verdicts and - if applicable - any sentencing would be in public.

And, they said they were not persuaded that there was a risk to the administration of justice warranting the anonymisation of the defendants, who were previously known only as AB and CD,

Lord Justice Gross said: "We express grave concern as to the cumulative effects of holding a criminal trial in camera and anonymising the defendants.

"We find it difficult to conceive of a situation where both departures from open justice will be justified. Suffice to say, we are not persuaded of any such justification in the present case."

He added that one issue canvassed before Mr Justice Nicol was whether a small number of "accredited journalists" might be invited to attend the bulk of the trial - subject to being excluded when a small number of matters were discussed - on terms which compelled confidentiality until review at the conclusion of the trial and any further order.

"The judge was not persuaded, essentially on grounds of practicality. We respectfully disagree. The arrangements can be agreed or can be dealt with in our order, if need be, following further brief argument.

"Any breach would obviously carry the likelihood of severe sanctions and, as has been observed on previous occasions, reliance must be placed on the responsibility of the media."

A spokesman for the Attorney General said later: "The principle of open justice is key to the British legal system and trials will always be held in public unless there are very strong reasons for doing otherwise.

"The measures applied for by the CPS in this case were, they believed, justified in order for the trial to proceed and for the defendants to hear the evidence against them while protecting national security.

"We are pleased that the court recognised the strength of some of these arguments, and that the case can go ahead. The CPS has indicated it accepts the judgment of the court, and will tailor its approach to the prosecution accordingly."

Incedal is charged with an offence contrary to section 5, Terrorism Act 2006 (preparation of terrorist acts) and an offence contrary to section 58, Terrorism Act 2000 (collection of information).

Isabella Sankey, director of policy for Liberty, said: "The judges are clear that open justice is a priceless foundation of our system and faced with a blacked-out trial we now have a few vital chinks of light.

"But their wholesale deference to vague and secret ministerial 'national security' claims is worrying. Shutting the door on the core of a criminal trial is a dangerous departure from our democratic tradition."

Sadiq Khan MP, Labour's shadow justice secretary, said: "I welcome the Court of Appeal judgment which has confirmed that the cloak of secrecy of the scale proposed is not acceptable. There may be exceptional circumstances that warrant parts of cases being held in secret, but to do so for an entire case would have been unprecedented.

"The Court of Appeal had all the facts of this particular case at their disposal and have ordered that parts of it could and should be held in the open. This judgment is a victory for the precious open and transparent nature of our justice system and public confidence will be enhanced as a result."

Incedal and Rarmoul-Bouhadjar, both 26 and from London, later appeared at the Old Bailey via video-link from Belmarsh prison.

The pair, sitting together in matching maroon T-shirts, spoke only to confirm their names at the start of the hearing.