In this code print (count) statement not display as well no xml-file create.why xml-file not creating is it issue with raise statement.
code-----
在这段代码中,print(Count)语句也不显示,没有创建XML文件。为什么没有创建XML文件是RAISE语句的问题。代码
import os
import xml.etree.ElementTree as ET
error_messages = []
count = 0
def sample_function():
try:
result = 10 / 0
except Exception as e:
print(f"An error occurred: {e}")
error_messages.append(str(e))
global count
count += 1
print(count)
raise Exception("An error occurred in the script.")
sample_function()
if count > 0:
print("Creating XML...")
root = ET.Element("errors")
subject = ET.Element("subject")
subject.text = "DS Interface-v2.2.1 - ERROR: Python Script"
salutation = ET.Element("salutation")
salutation.text = "Dear Sir/Madam,"
body = ET.Element("body")
logs_path = "${VMPROCESSES}\\TRUMPF\\DS-Interface-v2.2.1\\python\\logs"
body.text = f"The Process has not been finished successfully. Having some issues regarding the Python script. Logfile can be found at location {logs_path}"
root.append(subject)
root.append(salutation)
root.append(body)
for error_message in error_messages:
error_element = ET.Element("error")
error_element.text = error_message
root.append(error_element)
output_directory = "c:/Users/PC/Desktop/gathering for ds/all-try python file/sample"
os.makedirs(output_directory, exist_ok=True)
xml_file_path = os.path.join(output_directory, "sample1_messages.xml")
tree = ET.ElementTree(root)
tree.write(xml_file_path)
print(f"XML file created at {xml_file_path}")
why xml-file not creating is it issue with raise statement.
为什么不创建XML文件是RAISE语句的问题。
更多回答
Pretty sure that if an uncaught exception occurs, processing will stop.
可以肯定的是,如果发生未捕获的异常,处理将停止。
why do you divide 10 over 0 ?
为什么你要把10除以0呢?
The raise
statement causes another exception, and this one is not handled with a try/except block, so the program crashes.
RAISE语句会导致另一个异常,而这个异常不是用TRY/EXCEPT块处理的,因此程序崩溃。
In this code print (count) statement not display The print(count)
statement certainly should have worked...
在这段代码中,print(Count)语句没有显示print(Count)语句,当然应该可以工作……
优秀答案推荐
You can put your if statement in a finally block.
您可以将if语句放在Finally块中。
import os
import xml.etree.ElementTree as ET
error_messages = []
def sample_function(counter=0):
try:
result = 10 / 0
print(result)
except ZeroDivisionError as e:
error_messages.append(str(e))
print(e)
counter += 1
except Exception as e:
print(f"An error occurred: {e}")
error_messages.append(str(e))
counter += 1
raise Exception("An error occurred in the script.")
finally:
if counter > 0:
print("Creating XML...")
root = ET.Element("errors")
subject = ET.Element("subject")
subject.text = "DS Interface-v2.2.1 - ERROR: Python Script"
salutation = ET.Element("salutation")
salutation.text = "Dear Sir/Madam,"
body = ET.Element("body")
logs_path = "${VMPROCESSES}\\TRUMPF\\DS-Interface-v2.2.1\\python\\logs"
body.text = f"The Process has not been finished successfully. Having some issues regarding the Python script. Logfile can be found at location {logs_path}"
root.append(subject)
root.append(salutation)
root.append(body)
for error_message in error_messages:
error_element = ET.Element("error")
error_element.text = error_message
root.append(error_element)
ET.dump(root)
output_directory = "sample"
os.makedirs(output_directory, exist_ok=True)
xml_file_path = os.path.join(output_directory, "sample1_messages.xml")
tree = ET.ElementTree(root)
tree.write(xml_file_path)
print(f"XML file created at {xml_file_path}")
sample_function(0)
更多回答
我是一名优秀的程序员,十分优秀!