Python Ignore Exception and Go Back to Where I Was

后端 未结 7 1385
孤街浪徒
孤街浪徒 2021-02-13 12:47

I know using below code to ignore a certain exception, but how to let the code go back to where it got exception and keep executing? Say if the exception \'Exception\' raises in

7条回答
  •  感动是毒
    2021-02-13 13:10

    Exceptions are usually raised when a performing task can not be completed in a manner intended by the code due to certain reasons. This is usually raised as exceptions. Exceptions should be handled and not ignored. The whole idea of exception is that the program can not continue in the normal execution flow without abnormal results.

    What if you write a code to open a file and read it? What if this file does not exist?

    It is much better to raise exception. You can not read a file where none exists. What you can do is handle the exception, let the user know that no such file exists. What advantage would be obtained for continuing to read the file when a file could not be opened at all.

    In fact the above answers provided by Aaron works on the principle of handling your exceptions.

提交回复
热议问题