Tag: 线程exception

如果您在“我们不使用例外”阵营,那么您如何使用标准库?

注意:我不是在这里扮演魔鬼的拥护者或者类似的东西 – 我真的很好奇,因为我本人不在这个阵营。 标准库中的大多数types都具有可以抛出exception(例如,如果内存分配失败)的突变函数或可以抛出exception的非突变函数(例如超出索引访问器的范围)。 除此之外,许多免费函数可能会抛出exception(例如operator new和dynamic_cast<T&> )。 在“我们不使用例外”的情况下,你如何处理这个问题? 你想永远不要调用一个可以抛出的函数吗? (我看不出如何扩展,所以我很感兴趣,听听你是如何做到这一点的) 你可以使用标准库抛出,你把“我们不使用exception”当作“我们从不抛出exception,我们从来没有从其他代码中捕获exception”? 你是否通过编译器开关完全禁用exception处理? 如果是这样,标准库的exception抛出部分如何工作? 编辑你的构造函数,他们可以失败,或者你是否按惯例使用一个专用的初始化函数,可以返回一个失败的错误代码(构造函数不能),或者你做别的什么? 编辑在问题问世1周后进行小幅度的澄清……下面的评论和问题的大部分内容都集中在例外与“其他”的方面。 我的兴趣不在于此,但是当你select“别的东西”的时候,你如何处理那些抛出exception的标准库部分呢?

抛出RuntimeException的方法应该在方法签名中指明它吗?

例如,框架/ JDK中的许多方法可能会抛出 java.lang.SecurityException 但是这在方法签名中没有被指出(因为这是通常为检查exception保留的做法)。 我想争辩说,在方法sigs中声明RuntimeExceptions有很多好处(类似于静态types检查)。 我喝醉了吗?

是否可以尝试/抓住一些东西来检查是否抛出exception?

是否尝试一些无用的方法来查看这个代码是否抛出了一个特殊的exception? 抛出exception时我想做些什么,除此之外什么也不做。 try { new BigDecimal("some string"); // This do nothing because the instance is ignored } catch (NumberFormatException e) { return false; // OK, the string wasn't a well-formed decimal } return true; testing有太多的先决条件,构造函数BigDecimal()总是检查它们,所以这看起来是最简单的方法。

Python:我怎么知道哪些exception可能会从方法调用中抛出

有没有一种方法知道(在编码时)执行python代码期望的exception? 由于我不知道可能引发哪种exceptiontypes,所以我最终在90%的时间内捕获了基类exception类(并且不要告诉我阅读文档,很多时候exception可以从深处传播出来,次文档不更新或正确)。 有没有某种工具来检查这个? (如通过阅读Python代码和库)?

MVC2中的图像和robots.txt中出现“不执行IController”错误

我得到一个奇怪的错误在我的networking服务器看似每个文件,但.aspx文件。 这是一个例子。 只要将“/robots.txt”replace为任何.jpg名称或.gif或其他内容即可,您将会看到: path“/robots.txt”的控制器未find或未实现IController。 我相信这是如何设置路由,但我不知道我需要做什么。 另外,这是一个混合的MVC和WebForms网站,如果这有所作为。

elmah也处理exception

ELMAH是否logging了exception,即使它们没有向应用程序冒泡? 我想在发生exception时popup消息,并仍然loggingexception。 目前,我一直在试图抓住所有的信息,但是这会变得乏味。

如果不立即重新生成exception追踪,则隐藏

我有一个类似于这样的代码: import sys def func1(): func2() def func2(): raise Exception('test error') def main(): err = None try: func1() except: err = sys.exc_info()[1] pass # some extra processing, involving checking err details (if err is not None) # need to re-raise err so caller can do its own handling if err: raise err if __name__ == […]

我如何处理Python中的列表理解exception?

我有一些Python中的列表理解,其中每个迭代可以抛出一个exception。 例如 ,如果我有: eggs = (1,3,0,3,2) [1/egg for egg in eggs] 我会在第三个元素中得到一个ZeroDivisionErrorexception。 我如何处理这个exception并继续执行列表理解? 我能想到的唯一方法是使用一个辅助函数: def spam(egg): try: return 1/egg except ZeroDivisionError: # handle division by zero error # leave empty for now pass 但是这对我来说看起来有点麻烦。 有没有更好的方法来在Python中做到这一点? 注意:这是一个简单的例子(参见上面的“ 例如 ”),因为我的真实例子需要一些上下文。 我不想避免被零错误分割,而是在列表理解中处理exception。

如何在Java中抛出一个普通的exception?

考虑这个简单的程序。 该程序有两个文件: Vehicle.java: class Vehicle { private int speed = 0; private int maxSpeed = 100; public int getSpeed() { return speed; } public int getMaxSpeed() { return maxSpeed; } public void speedUp(int increment) { if(speed + increment > maxSpeed){ // throw exception }else{ speed += increment; } } public void speedDown(int decrement) { if(speed […]

InflateException与官方devise库中的FloatingActionButton

我正在使用Google支持devise库中的官方FloatingActionButton一个错误。 这是我的LogCat。 android.view.InflateException: Binary XML file line #34: Error inflating class android.support.design.widget.FloatingActionButton at android.view.LayoutInflater.createView(LayoutInflater.java:633) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method) at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:655) at android.view.LayoutInflater.inflate(Unknown Source) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at —.—.com.—.SubCategoryFragment.onCreateView(SubCategoryFragment.java:47) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at […]