He didn't have many options. Although we strongly criticize both invasions, I'll accept that they seemed reasonable at the time.
In the Maryland excursion, he had just severely cowed McClellan and dealt a sizeable smash to Pope's army. He had the initiative and exploited it. I still find it hard to believe that he expected recruits in Maryland, but apparently he did.
In the Pennsylvania Campaign, he had a do-or-die facing him. His food supply was being chipped away from him -- his food producers were feeding Yankees. He had just given Hooker a drubbing and the logical next step was to try to deliver another. An overly bold stroke was called for and he figured it would get more attention if it was delivered in a Union state. If he could whip the AotP one more time, maybe the inevitable end could be averted. Better to try than to sit back and wait.
He was being supplied, to a degree, but the vittles were getting skimpy and looked very much like they would continue to diminish. There was not a lot of corn grown in the Carolinas or Georgia. The TransMississippi, Kentucky and Tennessee were off-line by July of '63.
Lee had to do something bold. And he did. Things didn't work out as well as he'd hoped, but he still had to try.
ole