GVKun编程网logo

在尝试捕获中使用Throwable和Exception之间的区别(用于尝试捕捉可能出现的异常)

11

在本文中,我们将为您详细介绍在尝试捕获中使用Throwable和Exception之间的区别的相关知识,并且为您解答关于用于尝试捕捉可能出现的异常的疑问,此外,我们还会提供一些关于c#–Argumen

在本文中,我们将为您详细介绍在尝试捕获中使用Throwable和Exception之间的区别的相关知识,并且为您解答关于用于尝试捕捉可能出现的异常的疑问,此外,我们还会提供一些关于c# – ArgumentException和Exception之间有什么区别?、c# – 处理Application.ThreadException和在Try / Catch中包装Application.Run之间的区别、except: 和 except Exception as e 之间的区别:、Exception in thread "main" java.io.IOException: Unable to establish loopback connection的有用信息。

本文目录一览:

在尝试捕获中使用Throwable和Exception之间的区别(用于尝试捕捉可能出现的异常)

在尝试捕获中使用Throwable和Exception之间的区别(用于尝试捕捉可能出现的异常)

有时候,我看到

try {} catch(Throwable e) {}

而有时

try {} catch(Exception e) {}

有什么区别?

答案1

小编典典

通过捕获,Throwable它包含了子类化的东西Error。通常,您不应该这样做,除非可能是在您要记录的线程的最高“捕获所有”级别,或者绝对要处理可能出错的所有内容。这将是一个框架型应用程序(例如应用程序服务器或一个测试框架),它可以运行未知代码,不应受到影响比较典型的
事情 是去错代码,尽可能多地。

c# – ArgumentException和Exception之间有什么区别?

c# – ArgumentException和Exception之间有什么区别?

在我们教授的示例代码中,他有一个看起来像这样的片段:

if (name == null || name == "")
    throw new ArgumentException("name is null or empty");

另一个看起来像这样的片段:

if (!File.Exists(name))
{
    throw new Exception("File does not exist!");
}

我只是想知道不同之处是什么以及为什么一个在另一个之上使用

解决方法

Exception是所有异常的基类. ArgumentException用于表示参数无效.它是Exception的子类.使用catch,您实际上可以根据异常的类型进行过滤,并以不同的方式处理每个异常.

MSDN很好地描述了它:

When you have to throw an exception,you can often use an existing exception type in the .NET Framework instead of implementing a custom exception. You should use a standard exception type under these two conditions:

  • You are throwing an exception that is caused by a usage error (that is,by an error in program logic made by the developer who is calling your method). Typically,you would throw an exception such as ArgumentException,ArgumentNullException,InvalidOperationException,or NotSupportedException. The string you supply to the exception object’s constructor when instantiating the exception object should describe the error so that the developer can fix it. For more information,see the Message property.
  • You are handling an error that can be communicated to the caller with an existing .NET Framework exception. You should throw the most derived exception possible. For example,if a method requires an argument to be a valid member of an enumeration type,you should throw an InvalidEnumArgumentException (the most derived class) rather than an ArgumentException.

c# – 处理Application.ThreadException和在Try / Catch中包装Application.Run之间的区别

c# – 处理Application.ThreadException和在Try / Catch中包装Application.Run之间的区别

是否有任何理由更喜欢在 Windows窗体应用程序中实现全局异常处理程序的这些方法之一?

第一种方法

static void Main(string[] args) 
{
    try
    {
        System.Windows.Forms.Application.Run(mainform);
    }
    catch (Exception ex)
    {
        // Log error and display error message
    }
}

第二种方法

static void Main(string[] args) 
{
    System.Windows.Forms.Application.ThreadException += 
        new ThreadExceptionEventHandler(Application_ThreadException);
    System.Windows.Forms.Application.Run(mainform);
}

static void Application_ThreadException(object sender,ThreadExceptionEventArgs e)
{
    // Log error and display error message
}

处理ThreadException事件会给你一些try / catch没有的东西吗?

解决方法

我对这种行为的理解是,添加一个 ThreadException处理程序将导致表单中未处理的异常被此处理程序捕获和处理,这将允许应用程序继续运行.

在try / catch设计中,表单中的第一个未处理的异常将导致应用程序停止.您将捕获异常,但应用程序将结束.

请注意,在某些情况下还会引发一个AppDomain.UnhandledException事件(主窗口表单线程以外的线程中未处理的异常并且没有捕获ThreadException),所有这些都是对您的应用程序非常不利的消息.

except: 和 except Exception as e 之间的区别:

except: 和 except Exception as e 之间的区别:

以下两个代码片段都做同样的事情。他们捕获每个异常并执行except:块中的代码

片段 1 -

try:
    #some code that may throw an exception
except:
    #exception handling code

片段 2 -

try:
    #some code that may throw an exception
except Exception as e:
    #exception handling code

两种构造的具体区别是什么?

Exception in thread

Exception in thread "main" java.io.IOException: Unable to establish loopback connection



Exception in thread "main" java.io.IOException: Unable to establish loopback connection
at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:106)
at java.security.AccessController.doPrivileged(Native Method)
at sun.nio.ch.PipeImpl.<init>(PipeImpl.java:122)
at sun.nio.ch.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:27)
at java.nio.channels.Pipe.open(Pipe.java:133)
at sun.nio.ch.WindowsSelectorImpl.<init>(WindowsSelectorImpl.java:105)
at sun.nio.ch.WindowsSelectorProvider.openSelector(WindowsSelectorProvider.java:26)
at java.nio.channels.Selector.open(Selector.java:209)
at sun.net.httpserver.ServerImpl.<init>(ServerImpl.java:73)
at sun.net.httpserver.HttpServerImpl.<init>(HttpServerImpl.java:32)
at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(DefaultHttpServerProvider.java:17)
at com.sun.net.httpserver.HttpServer.create(HttpServer.java:111)
at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:304)
at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117)
at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92)
at com.test.webser.RESTfulHelloWorld.main(RESTfulHelloWorld.java:36)
Caused by: java.net.ConnectException: Connection timed out: connect
at sun.nio.ch.Net.connect(Native Method)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
at java.nio.channels.SocketChannel.open(SocketChannel.java:146)
at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:78)
... 15 more

今天关于在尝试捕获中使用Throwable和Exception之间的区别用于尝试捕捉可能出现的异常的分享就到这里,希望大家有所收获,若想了解更多关于c# – ArgumentException和Exception之间有什么区别?、c# – 处理Application.ThreadException和在Try / Catch中包装Application.Run之间的区别、except: 和 except Exception as e 之间的区别:、Exception in thread "main" java.io.IOException: Unable to establish loopback connection等相关知识,可以在本站进行查询。

本文标签: