In the U.S. legal system, a person can be found not guilty of a crime that the person, as a matter of fact, did commit, if the person is so severely mentally ill that they could not understand right from wrong. The defendant is then found ‘not guilty by reason of insanity’. However, in the legal system, according to my limited understanding, this is an all-or-nothing proposition. In morality, the situation is quite different; it is a matter of degrees.
Suppose that a person, as a matter of fact, has committed an act that is objectively a mortal sin. This objective mortal sin will then also be an actual mortal sin, deserving of eternal punishment if the person does not repent, if the sin was committed with full knowledge of the grave immorality of the act and with full freedom of choice (full deliberation). Any factors that substantially reduce fullness of knowledge or fullness of free choice, might reduce the culpability from actual mortal sin to actual venial sin, or might even remove the culpability, so that no actual sin was committed. If an objective mortal sin is committed without knowledge of its immorality (i.e. through invincible ignorance), or without freedom of choice, then the act is not an actual sin. Only actual sins have culpability and deserve punishment.
Mental illness is a matter of degree. In other words, mental health is a matter of degree. We could say the same about physical health. There are not two types of persons, the physically healthy and the physically unhealthy. It is a matter of degree. Culpability for sin is also a matter of degree.
A person could possibly commit an objective mortal sin with sufficient fullness of knowledge and choice so as to be guilty of actual mortal sin, even if the person has a degree of mental illness. The person might still have sufficient knowledge and freedom of choice to sin gravely and with full culpability. Then, too, mental illness might affect the person’s understanding of the morality of an act, to some degree, so that the full gravity of the act is not comprehended. But a small reduction in the person’s knowledge would reduce culpability only a little, and not so much as to make the act other than an actual mortal sin. A substantial reduction in the person’s understanding of the immorality of his act could reduce the culpability for an objective mortal sin to an actual venial sin, or remove culpability altogether.
Mental illness can also affect the freedom of choice with which a person acts. And the same considerations apply as with fullness of knowledge. If the person acted with reduced freedom of choice, due to mental illness, some culpability may remain, if the reduction is only a matter of degree. But if the mental illness is so severe that the sin is committed without true and free exercise of will, then the act is not an actual mortal sin, even if it is objectively a gravely immoral act.
Can a person be not guilty of sin by reason of insanity? Yes, this is possible. But it is perhaps more common that mental illness reduces culpability only to a degree, so that some guilt for the sins committed remains.