处理错误
 
 

Most development environments provide default error handling. For C# and VB.NET, the default reaction to an error is to display an error message and terminate the application. While this behavior is adequate during the development, it is not productive for the user.

大部分开发环境都提供默认的错误处理。对于 C# 和 VB.NET,对错误的默认反应是显示错误信息并终止应用程序。虽然这种处理方式在应用程序的开发阶段足以解决问题,但对最终用户来说效率并不高。

Errors raised during execution should be handled and not left to the user to encounter whenever possible. When designing an application, you should catch all possible errors and then determine how to respond to them. In some situations an error can be safely ignored, while in others you will need to handle the error with a specific response in order for the application to continue.

应该处理程序在执行期间产生的错误,而且尽可能不要让用户遇到。在设计应用程序时,应该捕捉所有可能的错误,然后决定如何响应它们。在某些情况下,一个错误可以被安全的忽略掉, 而在有些情况下为了程序能够继续运行,将需要使用特殊的响应来处理错误。

When you catch an error, the default error message is suppressed and the application does not automatically terminate. With the default error message suppressed, you can display a custom error message or have the application handle the error.

当捕捉到一个错误时,默认的错误信息将被禁止而且应用程序不能自动中止。由于默认错误信息被禁止,所以可以显示一个自定义的错误信息或者使用应用程序来处理错误。

In general, error handling is necessary whenever user input is required, during file I/O operations, and when a database or object is being accessed. Even if you are sure a file or object is available, there may be conditions you have not thought of that could cause errors.

一般来说,只要需要用户输入或操作文件 I/O,以及数据库或对象被访问时,就需要进行错误处理。请记住,即使确信所需文件位于正确位置并且可被处理,也可能出现没有考虑到的情况,从而导致出现错误。

NoteMost of the code examples provided in this documentation do not use error handling or the error handling is limited in scope. This keeps the examples simple and to the point. However, as with all programming languages, proper error handling is essential for a robust application.

注意AutoCAD 文档中提供的大多数代码样例都不使用错误捕获或将错误处理限制在一个范围内。这是为了让样例能够简洁扼要。不过,对于所有的编程语言,适当的错误捕获和处理,对于提高应用程序的强壮性是非常重要的。