如何捕获try-with-resource语句中close方法抛出的异常(How to catch exception thrown by close method in try-with-resource statement)
我正在阅读Java中的
try-with-resource
语句,它可用于指定任意数量的资源。try (Resource1 res1 = initialize_code; Resource1 res2 = initialize_code; ...) { statement; }
现在,当try块退出时(通常或异常抛出异常),将调用所有资源对象的
close
方法。 但是一些close
方法会抛出异常。 如果close
本身抛出异常,那么在那种情况下会发生什么?I was reading about
try-with-resource
statement in Java which can be used to specify any number of resources.try (Resource1 res1 = initialize_code; Resource1 res2 = initialize_code; ...) { statement; }
Now when the try block exits (normally or abnormally throwing an exception) the
close
methods of all resource objects are invoked. But someclose
methods can throw exceptions. What will happen in that scenario ifclose
itself throws exception?
原文:https://stackoverflow.com/questions/32753093
最满意答案
同
br.form['q'] = sys.argv
您在此处分配字符串列表 而不是字符串。
>>> type(sys.argv) <type 'list'> >>> type(sys.argv[0]) <type 'str'> >>>
您想要标识要通过索引分配的特定字符串。
鉴于你在帖子中的内容很可能是索引1(因为索引0是脚本的名称)。 也许吧
br.form['q'] = sys.argv[1]
会为你做的。 当然,它也可能是另一个索引,具体取决于您的特定应用/需求。
请注意@Dougal在下面的有用注释中观察到,函数中的函数参数
word
未被使用。 你正在调用你的dictionary
函数发送它sys.argv
然后应该引用函数内的word
。 该类型不会仅更改您在函数内引用命令行args的名称。word
的想法是好的,因为它避免使用全局变量。 如果你提到使用全局变量(不是真的鼓励),那么建议删除word
,因为它会让人感到困惑)。所以你的陈述应该真正阅读
br.form['q'] = word[1]
With
br.form['q'] = sys.argv
you are assigning a list of strings here instead of a string.
>>> type(sys.argv) <type 'list'> >>> type(sys.argv[0]) <type 'str'> >>>
You want to identify a specific string to assign via an index.
Most likely it will be be index 1 given what you have in your post (and since index 0 is the name of the script). So perhaps
br.form['q'] = sys.argv[1]
will do for you. Of course it could be another index too, depending on your particular application/needs.
Note as @Dougal observes in a helpful comment below, the function parameter
word
in the function is not being used. You are calling yourdictionary
function sending itsys.argv
and then ought to refer toword
inside the function. The type doesn't change only the name that you refer to the command line args inside your function. The idea ofword
is good as it avoids the use of global variables. If you refer to use globals (not really encouraged) then removingword
is recommended as it will be confusing to have it there).So your statement should really read
br.form['q'] = word[1]
相关问答
更多-
python: sys.argv的实际作用?[2022-02-05]
sys.argv 将你从命令行执行脚本时的 脚本名称、参数保存到列表中。 如在linux 命令行下执行:test.py -v 123, 则 sys.argv=[‘test.py’,'-v','123'] 这样你就可以在脚本'test.py'中使用这些参数了。 -
python关于sys.argv的使用[2022-03-23]
sys.argv是你接收的参数的列表 [] 比如你这段代码名字叫做test.py 运行python test.py 192.168.0.1 test.txt sys.argv[0] ----test.py sys.argv[1] ----192.168.0.1 sys.argv[2] ----test.txt 你可以print试试 你要用wing 直接运行 就需要设置一下 在运行设置里面加上命令 -
python和sys.argv(python and sys.argv)[2022-06-15]
BTW您可以将错误消息直接传递给sys.exit: if len(sys.argv) < 2: sys.exit('Usage: %s database-name' % sys.argv[0]) if not os.path.exists(sys.argv[1]): sys.exit('ERROR: Database %s was not found!' % sys.argv[1]) BTW you can pass the error message directly to sys.e ... -
我想注意,以前的答案对用户的知识做出了许多假设。 这个答案试图在更多的教程级别回答这个问题。 对于每次调用Python, sys.argv将自动列出表示命令行上参数(由空格分隔)的字符串。 该名称来自C编程惯例 ,其中argv和argc表示命令行参数。 当您熟悉Python时,您将想要了解更多关于列表和字符串的信息,但同时也需要了解一些事情。 您可以简单地创建一个脚本来显示参数。 它还打印参数的数量,使用列表中的len函数。 from __future__ import print_function imp ...
-
你的问题是python不知道函数目录的存在。 如果您尝试从子目录运行脚本,请执行此操作 function |_function.py | input_folder |_input.txt | |output_folder |_output.txt 你必须告诉python函数文件夹是本地的,所以 python ./function/function.py ./input_folder/input.txt ./output_folder/output.txt 要么 python $PWD/functio ...
-
使用'python pw.py email'作为第一个脚本和第二个'python pw.py any_args' Use ' python pw.py email ' for the first script and for the second 'python pw.py any_args'
-
sys.argv是一个参数列表。 因此,当您执行没有任何参数的脚本时,您正在访问索引1的列表为空,访问空列表的索引将引发IndexError 。 使用第二个代码块,您正在执行列表切片,并且您正在检查索引1和转发的列表切片是否为空,然后打印列表中的那个切片。 为什么这样做是因为如果你有一个空列表并像这样对它做一个切片,那么切片会返回一个空列表。 last_list = [1,2,3] if last_list[1:]: print last_list[1:] >> [2,3] empty_list ...
-
带有打印功能结果的Python sys.argv TypeErrors?(Python sys.argv TypeErrors with printing function results?)[2022-06-13]
同 br.form['q'] = sys.argv 您在此处分配字符串列表 而不是字符串。 >>> type(sys.argv)>>> type(sys.argv[0]) >>> 您想要标识要通过索引分配的特定字符串。 鉴于你在帖子中的内容很可能是索引1(因为索引0是脚本的名称)。 也许吧 br.form['q'] = sys.argv[1] 会为你做的。 当然,它也可能是另一个索引,具体取决于您的特定应用/需求。 请注意@Dougal在下面的有用注 ... -
因为您只传递了三个参数,所以下面的示例应该可以帮助您理解: >>> [1,2,3][-1] # working 3 >>> [1,2,3][-2] # working 2 >>> [1,2,3][-3] # working 1 >>> [1,2,3][-4] # exception as in your code Traceback (most recent call last): File "
", line 1, in IndexError: l ... -
这是一个特定于Windows的问题:尝试“python back.py examplearg”它会起作用。 解决方案(除了没有运行Windows!)是更新您的注册表。 http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/ 设置这些键=值 HKEY_CLASSES_ROOT \ Applications \ python26.exe \ shell \ open \ com ...