我如何知道Delphi函数可能抛出哪些异常? [英] How can I find out which exceptions a Delphi function might throw?

查看:161
本文介绍了我如何知道Delphi函数可能抛出哪些异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个很好的方法来找出Delphi中过程/函数可以引发哪些异常(包括它被称为过程/函数)?



在Java中,您始终必须声明可以抛出哪些异常,但Delphi中并不是这样,这可能会导致未处理的异常。



是否有任何代码分析工具可以检测未处理的异常?

解决方案

(编辑:现在显而易见的是,这个问题只涉及到设计时检查。)



新的回答:



我无法确定是否有任何工具来检查这一点。 Pascal分析器,一个,不是。



我可以告诉你,然而,在大多数Delphi应用程序中,即使有一个工具为了检查这个,你不会得到结果。



为什么?因为TApplication.Run()中的主要消息循环包装了所有的HandleMessage ()调用异常处理块,它捕获所有异常类型。因此,在大多数应用程序中,您将隐式/默认异常处理大约99.999%的代码。而在大多数应用程序中,这种异常处理将是您自己代码的大约100% - 不包含在异常处理中的代码的0.001%将是自动生成的代码。



如果有一个工具可以检查这个,你需要重写Application.run(),使其不包括异常处理。



(上一个答案:
可以分配Application.OnException事件处理程序来捕获所有未被其他异常处理程序处理的异常。虽然这是运行时,也可能并不完全符合您的要求听起来像你想在设计时识别它们),它允许你捕获任何不在其他地方处理的异常。结合工具,例如在绝地代码库,您可以记录一个堆栈跟踪,以了解哪里和为什么发生异常,这将允许进一​​步的调查并在有罪的代码附近添加特定的异常处理或预防...


Is there a good way to find out which exceptions a procedure/function can raise in Delphi (including it's called procedures/functions)?

In Java you always have to declare which exceptions that can be thrown, but this is not the case in Delphi, which could lead to unhandled exceptions.

Are there any code analysis tools that detects unhandled exceptions?

解决方案

(Edit: It is now obvious that the question referred only to design-time checking.)

New answer:

I cannot state whether there are any tools to check this for you. Pascal Analyzer, for one, does not.

I can tell you, however, that in most Delphi applications, even if there was a tool to check this for you, you would get no results.

Why? Because the main message loop in TApplication.Run() wraps all HandleMessage() calls in an exception handling block, which catches all exception types. Thus you will have implicit/default exception handling around 99.999% of code in most applications. And in most applications, this exception handling will be around 100% of your own code - the 0.001% of code which is not wrapped in exception handling will be the automatically generated code.

If there was a tool available to check this for you, you would need to rewrite Application.run() such that it does not include exception handling.

(Previous answer: The Application.OnException event handler can be assigned to catch all exceptions that aren't handled by other exception handlers. Whilst this is run-time, and thus perhaps not exactly what you are after (it sounds like you want to identify them at design time), it does allow you to trap any exception not handled elsewhere. In conjunction with tools such as the JCLDebug stuff in the Jedi Code Library, you could log a stack trace to find out where & why an exception occurred, which would allow for further investigation and adding specific exception handling or prevention around the guilty code...)

这篇关于我如何知道Delphi函数可能抛出哪些异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆