Answers for "how to print error in catch in python"

21

how to print error in try except python

try:
  # some code
except Exception as e:
	print("ERROR : "+str(e))
Posted by: Guest on November-19-2020
0

python catch print

from io import StringIO 
import sys

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stdout = self._stdout
        
with Capturing() as output:
    print('hello world')

print('displays on screen')

with Capturing(output) as output:  # note the constructor argument
    print('hello world2')

print('done')
print('output:', output)
Posted by: Guest on December-24-2021

Code answers related to "how to print error in catch in python"

Python Answers by Framework

Browse Popular Code Answers by Language