在这篇文章中,我们将为您详细介绍在使用Python'with'语句时捕获异常的内容,并且讨论关于在python中,可以用异常处理捕获程序中的所有错误的相关问题。此外,我们还会涉及一些关于android
在这篇文章中,我们将为您详细介绍在使用 Python 'with' 语句时捕获异常的内容,并且讨论关于在python中,可以用异常处理捕获程序中的所有错误的相关问题。此外,我们还会涉及一些关于android – 如何在使用LinkMovementMethod时捕获异常(ActivityNotFoundException)、Java中的异常处理:何时抛出异常,何时捕获异常、perl – 如何在使用open / print执行SQL语句时捕获退出代码?、php – 使用AR findAll函数在使用with语句时只返回一个对象的知识,以帮助您更全面地了解这个主题。
本文目录一览:- 在使用 Python 'with' 语句时捕获异常(在python中,可以用异常处理捕获程序中的所有错误)
- android – 如何在使用LinkMovementMethod时捕获异常(ActivityNotFoundException)
- Java中的异常处理:何时抛出异常,何时捕获异常
- perl – 如何在使用open / print执行SQL语句时捕获退出代码?
- php – 使用AR findAll函数在使用with语句时只返回一个对象
在使用 Python 'with' 语句时捕获异常(在python中,可以用异常处理捕获程序中的所有错误)
令我感到羞耻的是,我不知道如何处理 python ‘with’ 语句的异常。如果我有代码:
with open("a.txt") as f: print f.readlines()
我真的很想处理“文件未找到异常”以做某事。但我不会写
with open("a.txt") as f: print f.readlines()except: print ''oops''
并且不能写
with open("a.txt") as f: print f.readlines()else: print ''oops''
在 try/except 语句中包含 ‘with’ 不起作用:不会引发异常。为了以 Pythonic 方式处理“with”语句中的失败,我该怎么做?
答案1
小编典典from __future__ import with_statementtry: with open( "a.txt" ) as f : print f.readlines()except EnvironmentError: # parent of IOError, OSError *and* WindowsError where available print ''oops''
如果您想要对公开调用与工作代码中的错误进行不同的处理,您可以执行以下操作:
try: f = open(''foo.txt'')except IOError: print(''error'')else: with f: print f.readlines()
android – 如何在使用LinkMovementMethod时捕获异常(ActivityNotFoundException)
我注意到我们可以使用setMovementMethod使这些链接可以点击:
tv.setText("Phone number is: +32485123456 and url is http://www.blabla.com"); tv.setMovementMethod(LinkMovementMethod.getInstance());
这非常有效,但在某些设备上,它会因ActivityNotFoundException而崩溃,这是非常难以理解的. (见下面的代码)
这些问题出现在我的开发者控制台中,我无法重现它们.
我面临的一个大问题是我无法在我的活动中尝试/捕获代码,因为点击是由LinkMovementMethod处理的
关于如何避免此类错误的任何想法?
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.vlaamsbrabant.be/zoutleeuw (has extras) } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) at android.app.Activity.startActivityForResult(Activity.java:2817) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817) at android.app.Activity.startActivity(Activity.java:2923) at android.text.style.URLSpan.onClick(URLSpan.java:62) at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216) at android.widget.TextView.onTouchEvent(TextView.java:6788) at android.view.View.dispatchTouchEvent(View.java:3766) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at com.android.internal.policy.impl.PhoneWindow$DecorView.superdispatchTouchEvent(PhoneWindow.java:1896) at com.android.internal.policy.impl.PhoneWindow.superdispatchTouchEvent(PhoneWindow.java:1159) at android.app.Activity.dispatchTouchEvent(Activity.java:2086) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1880) at android.view.ViewRoot.handleMessage(ViewRoot.java:1811) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4627) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method)
解决方法
就像是:
private class MovementCheck extends LinkMovementMethod { @Override public boolean onTouchEvent( TextView widget,Spannable buffer,MotionEvent event ) { try { return super.onTouchEvent( widget,buffer,event ) ; } catch( Exception ex ) { Toast.makeText( MainActivity.this,"Could not load link",Toast.LENGTH_LONG ).show(); return true; } } }
如果没有应用程序设置来处理意图,将显示错误消息.
另一种选择是使用this question中的方法提前确定是否准备好处理链接的任何内容.
Java中的异常处理:何时抛出异常,何时捕获异常
今天在看hadoop源码时,想想自己最近在做的那个系统,发现很多异常处理的方式不对,还是按照传统的异常处理方式(即:采用返回值来标识程序出现的异常情况)。而hadoop中很多方法的声明是有异常抛出的,而我的系统中的很多方法的声明都没有抛出异常。只是判断了异常情况,并输出了错误提示,但是并没有抛出异常。
org.apache.hadoop.hdfs.protocol包下的Block类的readFields()方法:
public void readFields(DataInput in) throws IOException {
this.blockId = in.readLong();
this.numBytes = in.readLong();
this.generationStamp = in.readLong();
if (numBytes < 0) {
throw new IOException("Unexpected block size: " + numBytes);//抛出异常,要是的话就不会抛出,而只是System.out.println错误提示,
}
1.如果方法声明名里面有throws异常,那么方法体里面可以不抛出异常。因为可以在方法声明中包含异常说明,但实际上却不抛出!这样做的好处是,为异常先占个位置,以后就可以抛出这种异常而不用修改修改已有的代码。在定义抽象基类和接口时这种能力很重要,这样派生类或接口实现类就能够抛出这些预先声明的异常。
2.为什么有的方法声明里面没有throws,但方法体里面却抛出了异常?从RuntimeException继承的异常,可以在没有异常说明throws的情况下被抛出!对于Runtime异常(也称为非检查的异常unchecked exception),编译器不需要异常说明。只能在代码中忽略RuntimeException(及其子类)类型的异常,其他类型的异常的处理都是由编译器强制实施的。究其原因,RuntimeException代表的是编程错误。
3.运行时异常会被Java虚拟机自动抛出!
《有效处理Java异常三原则》http://www.importnew.com/1701.html
- 点赞 2
- 收藏
- 分享
-
- 文章举报

perl – 如何在使用open / print执行SQL语句时捕获退出代码?
open (PLsql,"|sqlplus -s $db_url"); print PLsql <<ENDsql; ... some sql statements ... exit; ENDsql close(PLsql);
我的问题是如果遇到一些错误,如何捕获执行sql语句的退出代码.我认为使用DBI会好得多,但我更愿意找到解决上述问题的方法.非常感谢!
解决方法
close()
应该告诉你你想知道什么:
If the filehandle came from a piped open,close returns false if one of the other syscalls involved fails or if its program exits with non-zero status. If the only problem was that the program exited non-zero,$! will be set to 0 . Closing a pipe also waits for the process executing on the pipe to exit–in case you wish to look at the output of the pipe afterwards–and implicitly puts the exit status value of that command into $? and ${^CHILD_ERROR_NATIVE}.
要点是close()将为任何错误返回false,$!只有在系统调用有错误时才会设置,并且$?将被设置为退出状态.有关详情,请参阅Error Variables in perlvar.
php – 使用AR findAll函数在使用with语句时只返回一个对象
价钱:
class Pricing extends CActiveRecord { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return Pricing the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'pricing'; } /** * @return string the primary key */ public function primaryKey(){ return 'ID'; } ... /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'xpricing_routes' => array(self::HAS_MANY,'PricingRoutes','ID_pricing'),); }
和PricingRoutes:
class PricingRoutes extends CActiveRecord { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return PricingRoutes the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'pricing_routes'; } /** * @return string the primary key */ public function primaryKey(){ return 'ID'; } ... /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'xpricing' => array(self::BELONGS_TO,'Pricing',); }
然后在控制器中我们有:
$criteria = new CDbCriteria; $criteria->with = array('xpricing_routes'); $criteria->together=true; $pricing_records = Pricing::model()->findAll($criteria); $pricing_records_arr = CHtml::listData($pricing_records,'id','name'); echo '<pre>'; print_r($pricing_records); print_r($pricing_record_arr); echo '</pre>';
您可能已经知道,我们有2个名为pricing和pricing_routes的表.定价路由表有一个名为ID_pricing的外键,它转到定价表中的ID字段.定价表有一个条目,pricing_routes表有4个条目,它们都具有ID_pricing字段中定价表中一个项目的主键.所以我们应该得到4个结果给我们正在运行的查询,当我运行Yii用AR生成的查询时,这就是我得到的.
我们遇到的问题是$pricing_records变量是一个只有一个Pricing对象的数组.该对象包含我们需要的数据,但不是真正可用的数据. $pricing_records_arr变量只是一个空数组.我找到的文档似乎表明我们应该获得一组定价对象,每个定价对象都包含来自pricing_routes表的信息.我们知道使用AR可能不是获取此数据的最佳方式,但我们有理由让这个工作,所以任何有关如何做到这一点的想法将非常感激.
编辑:
事实证明,这最终误解了我的回归.对这个问题的评论给了我所需要的信息.
解决方法
$pricing_records = Pricing::model()->findAll($criteria);
您将只获得一个活动记录,其中的属性填充了表“定价”中的值.
如果您想从“pricing_routes”中获取属于此特定“定价”的所有记录,则必须致电
$pricing_records->xpricing_routes
其中“xpricing_routes”是您在模型中正确定义的关系的名称.它返回PricingRoutes数组,如果没有匹配的记录,则返回null.
关于在使用 Python 'with' 语句时捕获异常和在python中,可以用异常处理捕获程序中的所有错误的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于android – 如何在使用LinkMovementMethod时捕获异常(ActivityNotFoundException)、Java中的异常处理:何时抛出异常,何时捕获异常、perl – 如何在使用open / print执行SQL语句时捕获退出代码?、php – 使用AR findAll函数在使用with语句时只返回一个对象的相关知识,请在本站寻找。
本文标签: