草庐IT

Try-catch

全部标签

java - 在预期的情况下处理 NumberFormatException 的正确方法是什么?

我遇到了这种情况,我需要将String解析为int并且我不知道如何处理NumberFormatException。当我没有捕捉到它时,编译器不会提示,但我只是想确保我正确地处理了这种情况。privateintgetCurrentPieceAsInt(){inti=0;try{i=Integer.parseInt(this.getCurrentPiece());}catch(NumberFormatExceptione){i=0;}returni;}我只想像这样简化我的代码。编译器对此没有问题,但线程在NumberFormatException上终止。privateintgetCurre

java - java 中的 Arrays.copyOfRange 方法抛出不正确的异常

我今天在做数组,突然遇到一个抛出意外异常的场景。如果您查看下面的代码,我认为它必须抛出ArrayIndexOutOfBoundsException,但令人惊讶的是它抛出了IllegalArgumentException:importjava.util.Arrays;publicclassRangeTest{publicstaticvoidmain(String[]args){int[]a=newint[]{0,1,2,3,4,5,6,7,8,9};int[]b=Arrays.copyOfRange(a,Integer.MIN_VALUE,10);//Ifwe'lluseInteger.

java - 如何避免重复复杂的 catch block

我有这个代码:try{do_stuff();returndo_more_stuff();}catch(UnsupportedEncodingExceptione){throwCustomException.programmer_error(e);}catch(ProtocolExceptione){throwCustomException.programmer_error(e);}catch(MalformedURLExceptione){throwCustomException.programmer_error(e);}catch(SocketTimeoutExceptione){t

java - 我可以避免这种麻烦的 try...catch block 吗

通常在处理JavaIO代码时,我是这样写的FileOutputStreamout=null;try{out=newFileOutputStream("myfile.txt");//Moreandmorecodegoeshere...}catch(Exceptione){}finally{//Iputtheclosecodeinfinallyblock,toenturetheopened//filestreamisalwaysclosedeventhereisexceptionhappened.if(out!=null){//Anothertrycatchblock,troublesom

python - 删除 Python 标准库中的变量

我一直在阅读标准threading库(Python2.6)中的一些代码,其中有一段代码让我感到奇怪。它可以缩短为以下结构(与threading.py中的__bootstrap_inner方法比较):deffoo():exc_type,exc_value,exc_tb=sys.exc_info()try:#somecodeexcept:#somecodefinally:delexc_type,exc_value,exc_tb这些变量不会超出foo范围。有没有理由在最后删除这些引用? 最佳答案 是的,至少对于exc_tb;回溯对象持有对

python - 将异常主体存储在变量中

有没有办法执行try语句并将错误主体作为变量返回?即var=''try:errorgeneratingcodeexcept:var=exception_body 最佳答案 是的,使用except的as语法:try:raiseException("helloworld")exceptExceptionasx:print(x)在早期的Python版本中,这将写成exceptException,x:,您可能会不时看到。 关于python-将异常主体存储在变量中,我们在StackOverflow

python - Windows 上的 GeoDjango : Try setting GDAL_LIBRARY_PATH in your settings

我以前这样做过十几次,但这次有些东西不工作..遵循文档:https://docs.djangoproject.com/en/1.11/ref/contrib/gis/install/#windows我正在尝试在Windows机器上设置GeoDjango(这是在paperspace.com上设置的虚拟Windows10)。我的PATH设置似乎有问题,但我无法弄清楚它是什么。我已经运行了说明中突出显示的命令。我检查了我的PATH变量,一切似乎都正常。我已经尝试将它们指向OSGeo4Win的32位和64位版本。无论如何,我每次都会得到以下输出:C:\Python\lib\site-packa

python - 仅捕获 Python 中的一些运行时错误

我正在导入一个在某些情况下会引发以下错误的模块:运行时错误:pyparted需要root访问权限我知道我可以在导入之前检查root访问权限,但我想知道如何通过try/except语句捕获这种特定类型的错误以供将来引用。有什么方法可以区分此RuntimeError和其他可能引发的错误吗? 最佳答案 您可以检查异常的属性以区别于其他可能的RuntimeError异常。例如,如果它与预定义的消息文本不匹配,则重新引发错误。try:importpypatredexceptRuntimeError,e:ife.message=='Runtim

python - 为什么 else 在 for/while 语句中的行为与 if/try 语句不同?

我最近偶然发现Python在处理不同复合语句中的else子句的方式中似乎存在不一致。由于Python的设计如此之好,我确信有一个很好的解释,但我想不出。考虑以下几点:ifcondition:do_something()else:do_something_else()此处,do_something_else()仅在condition为false时执行,正如预期的那样。同样,在try:do_something()exceptsomeException:pass:else:do_something_else()finally:cleanup()do_something_else()仅在未发生

python: try/except/else and continue 语句

为什么下面的python代码片段的输出NOT只是Noexception:1,因为在第一次迭代期间没有引发异常。来自python文档(https://docs.python.org/2.7/tutorial/errors.html)。Thetry...exceptstatementhasanoptionalelseclause,which,whenpresent,mustfollowallexceptclauses.Itisusefulforcodethatmustbeexecutedifthetryclausedoesnotraiseanexception.$cathello.pyfo