首页 \ 问答 \ 如何捕获try-with-resource语句中close方法抛出的异常(How to catch exception thrown by close method in try-with-resource statement)

如何捕获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 some close methods can throw exceptions. What will happen in that scenario if close itself throws exception?


原文:https://stackoverflow.com/questions/32753093
更新时间:2023-08-28 19:08

最满意答案

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 your dictionary function sending it sys.argv and then ought to refer to word inside the function. The type doesn't change only the name that you refer to the command line args inside your function. The idea of word is good as it avoids the use of global variables. If you refer to use globals (not really encouraged) then removing word is recommended as it will be confusing to have it there).

So your statement should really read

br.form['q'] = word[1]

相关问答

更多
  • sys.argv 将你从命令行执行脚本时的 脚本名称、参数保存到列表中。 如在linux 命令行下执行:test.py -v 123, 则 sys.argv=[‘test.py’,'-v','123'] 这样你就可以在脚本'test.py'中使用这些参数了。
  • 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 直接运行 就需要设置一下 在运行设置里面加上命令
  • 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 ...
  • 同 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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。