草庐IT

Try-Catch-Finally

全部标签

java - 在 try-catch block 中放入多少代码

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭9年前。Improvethisquestion是否有关于在try/catchblock中放入多少代码的“最佳实践”?我在下面发布了3种不同的场景。我没有在每个catchblock中包含行为,也没有包含finallyblock。这是为了提高观众的可读性。假设每个catch做不同的事情。并假设finally将关闭流。只是想为future的读者创建一个易于阅读的示例。控制,没有try/catch。为每个需要的地方编写1个try/catch。

java - 如何从 try、catch 和 finally 中返回一个值?

所以当我在try{}中执行代码块,并尝试返回一个值时,它告诉我noreturnvaluesimportorg.w3c.dom.ranges.RangeException;publicclassPg257E5{publicstaticvoidmain(String[]args){try{System.out.println(add(args));}catch(RangeExceptione){e.printStackTrace();}finally{System.out.println("Thanksforusingtheprogramkiddo!");}}publicstaticdou

java - 这个包含 close() 调用的 finally 子句的原因是什么

我正在学习在线java类(class),IntroductiontoprogrammingUsingJava.在关于I/O的章节中,下面的代码使用以下语句进行介绍:Bytheway,attheendofthisprogram,you'llfindourfirstusefulexampleofafinallyclauseinatrystatement.Whenthecomputerexecutesatrystatement,thecommandsinitsfinallyclauseareguaranteedtobeexecuted,nomatterwhat.程序在11.2.1节末尾并且是

java - if(null check)-else vs try catch(NullPointerException) 哪个效率更高?

下面三个函数哪个效率更高;publicStringgetmConnectedDeviceName1(){if(null!=mServerDevice){returnmServerDevice.getName();}else{returnnull;}}publicStringgetmConnectedDeviceName2(){returnmServerDevice==null?null:mServerDevice.getName();}publicStringgetmConnectedDeviceName3(){try{returnmServerDevice.getName();}ca

Java异步异常: Can I catch them?

我一直在阅读JLS,我遇到了11.1.3.AsynchronousExceptions部分我从中引用:Mostexceptionsoccursynchronouslyasaresultofanactionbythethreadinwhichtheyoccur,andatapointintheprogramthatisspecifiedtopossiblyresultinsuchanexception.Anasynchronousexceptionis,bycontrast,anexceptionthatcanpotentiallyoccuratanypointintheexecutio

java - Jersey / jackson : how to catch json mapping exception?

如果输入的json无效,我想在我的restful服务中捕获json映射异常。它抛出org.codehaus.jackson.map.JsonMappingException,但我不知道如何或在哪里捕获此异常。我想捕获此异常并发回适当的错误响应。@JsonInclude(JsonInclude.Include.NON_NULL)@Generated("org.jsonschema2pojo")@JsonPropertyOrder({"name","id"})publicclassCustomer{@JsonProperty("name")privateStringname;@JsonPr

python - OSError : [Errno 22] when I try to . read() 一个 json 文件

我只是想用Python读取我的json文件。当我这样做时,我在正确的文件夹中;我在下载中,我的文件名为“Books_5.json”。但是,当我尝试使用.read()函数时,出现错误OSError:[Errno22]Invalidargument这是我的代码:importjsonconfig=json.loads(open('Books_5.json').read())这也会引发同样的错误:books=open('Books_5.json').read()如果有帮助,这是我的数据的一小段:{"reviewerID":"A10000012B7CGYKOMPQ4L","asin":"0001

python - 在使用 try 和 except 语句赋值之前引用的局部变量

这个问题在这里已经有了答案:localvariable'servers'referencedbeforeassignment(3个答案)关闭9年前。我在try和except语句中遇到了一些问题,我有一个输入字符串的条目小部件,但我有稍后将其转换为整数的代码,问题是如果用户输入类似文本的内容,它会抛出像这样的错误:ExceptioninTkintercallbackTraceback(mostrecentcalllast):File"/usr/lib/python3.2/tkinter/__init__.py",line1402,in__call__returnself.func(*ar

python - 使用 Try & Except Python 2.7.1 的文件打开功能

defFileCheck(fn):try:fn=open("TestFile.txt","U")exceptIOError:print"Error:Filedoesnotappeartoexist."return0我正在尝试制作一个函数来检查文件是否存在,如果不存在,则它应该打印错误消息并返回0。为什么这不起作用??? 最佳答案 如果你想从exceptblock中返回,你需要缩进return0。另外,您的论点没有做任何事情。我假设您希望此函数能够测试任何文件,而不是为其分配文件句柄?如果不是,则不需要任何参数。defFileChec

python - Python 中嵌套的 try/except

try:commandstry:commandstry:commandstry:commandsexcept:commandsreturntofinalcommandsexcept:commandsreturntofinalcommandsexcept:commandsreturntofinalcommandsexcept:commandsfinalcommands我应该编写哪条指令来代替returntofinalcommands以使任何except都在外部try之后返回到顶级指令?它是可接受的结构吗?编辑:这是一个玩具示例(我知道我可以使用if来完成它,这只是一个示例;假设您必须使用