Exceptions and Error Codes

Source: Internet
Author: User

The beauty of returning a pointer to an object, is that you can return NULL as your standard error return value, and if the value mattered, you will most likely take the necessary steps to check its return value, or they application will just crash the moment you try to use it.

This coshould be applied to. NET and Java APIs to reduce the amount of required try/catchs. for example, the File. IO. openRead method cocould be (shown here is the artist's representation ):

 public static FileStream OpenRead (string file)
 {
         int fd = open (file);
         
         if (f == -1)
                 return null;
         return new FileStreamFromFD (f);
 }

Which makes null a valid return value. if you fail to check for the return value, you wocould get a nice NullReferenceException, but you wocould allow the expensive creation of an exception object, and the uugly try/catch block in the call site.

Obviusly we can not change the existing APIs, but we cocould encourage developers to minimize their use of exceptions. in my limited personal experience with software design, when I have faced applications with tons of limits it was extremely hard to keep up with them. A rewrite of the software to avoid exceptions and use the more sane API paid.

Here is the rationale for exceptions rather than error codesPredictionalSituations. (thanks to Jason Clark for writting these up for us)

There are dropping benefits to exception handling as compared to return value based error reporting. in fact, the Framework exposes more of the benefits of exception handling then some other common exception models such as Win32 SEH or C ++ exception handling. good managed API design does not restrict the application developer from realizing the benefits of limits. the following are some of these benefits.

Exceptions Promote API Consistency

Consistency of API is important to developers. exceptions promote consistency, because they are designed to be used for failure reporting (and nothing else ). in contrast, return values have passed uses of which failure reporting is only a subset. for this reason, it is likely that APIs that report failure through return values will find a number of patterns, while exceptions can be constrained to specific patterns. the Win32 API is a clear example of this inconsistency through BOOLs, HRESULTS, and GetLastError () among others.

Exceptions are Compatible with Object Oriented Features

Object-Oriented versions ages tend to impose constraints on method signatures that are not imposed by functions in functional programming. for example, constructor methods, operator overloads, virtual methods, generic methods, interface methods and properties all resolve to functions, and yet the developer writing the method has no choice in the return value (if any) of the method. for this reason, it is not possible to standardize on return value based error reporting for object oriented APIs. an error reporting method, such as exceptions, which is out of band of the method signature is the only option.

With Exceptions, Error Handling Code Need not be Near Failing Code

With return-value based error reporting error handling code is always very near to the code that coshould fail. however, with exception handling the application developer has a choice. code can be written to catch exceptions near the failure point, or the handling code can be further up in the call stack.

With Exceptions, Error Handling Code is More Localized

Very robust code that reports failure through return values tends to have an if statement for every functional line of code. the job of these if statements is to handle the failure case. with exception oriented code, robust code can often be written such that a number of methods and operations can be stored Med with the error handling logic grouped at the end of the try block (or even higher in the call-stack).

A common argument is that exception handling code is unsightly. This argument is usually being made by someone comparing well written exception handling code with return value oriented code that is not sufficiently checking return values of functions.

Specified tions Are Not Easily Ignored

Return values can be easily ignored, and often are. exceptions, on the other hand, take an active role in the flow of your code. this makes failures reported as exceptions difficult to ignore, and improves the robustness of code.
For example the Win32 API CloseHandle () fails very rarely, and so it is common (and appropriate) for other applications to ignore the return value of the API. however, if any application has a bug which causes CloseHandle () to be called twice on the same handle, it wocould not be obvious unless the code was written to test the return value. A managed equivalent to this API wocould throw an exception in this case and the unhandled exception handler wocould log the failure and the bug wocould be found.

Exceptions Allow for Unhandled Exception Handlers

Ideally, every application is written to intelligently handle all forms of failure. however, this is not realistic as all forms of failure can't be known. with return value oriented code, failures that are unexpected are ignored by code and the application continues to run with undefined results. with well written exception based code, unexpected failures eventually cause an unhandled exception handler to be called. this handler can be designed to log the failure, and can also make the choice to shut down the application. this is far preferable to running with indeterminate results, and also provides for logging that makes it possible to add more significant error handling for the previusly unexpected case. (Today, Microsoft Office uses an unhandled exception handler to gracefully recover and re-launch the application as well as send error information to Microsoft to improve the product .)

You shoshould only have a custom unhandled exception handler, if you have a have specific, app-oriented work to do in the handler. if you just want error reporting to occur, the runtime will handle that automatically for you and you do not need to (and shocould not) use a UEF. an application shoshould only register a UEF if it can provide functionality above and beyond the standard error reporting that comes with the system and runtime.

Exceptions Provide Robust Error Info for Logging

Exceptions provide for a consistent error handling mechanism, and as such make it possible to have consistent logging of error information. the System. exception type in the Framework encapsulates stack-frame information as well as other useful information such as a nested exception type. this is great for error logging and discovery of unexpected error cases during the QA phase. exceptions are objects, so it is possible to extend exceptions to add additional information.

Handling Errors Consistently and Thoroughly are Essential for Creating a Good User Experience

The application developer needs to provide specific information about what error occurred, why it occurred, and what can be done to mitigate it. this requires detailed and specific error returns from api cils. the exception mechanic allows detailed error information to be created by the API developer. there is no need for the API developer to change global header files to add new error codes. the API developer can customize the exception subclass as much as necessary to transmit all of the details of the error to the caller.

Exceptions Promote Instrumentation

Exceptions are a well-defined method-failure model. because of this, it is possible for tools such as debuggers, profilers, performance counters, and others to be intimately aware of exceptions. the PerfMon application, for example, keeps track of exception statistics. in the future automatic instrumentation from profiling to auto-documentation will be more sophisticated related to exceptions. methods that return failure do not share in the benefits of instrumentation.

Exceptions Unify the Error Model (Hard and Logical Errors)

Exception handling or interrupts are the only option for hard errors such as null references and divisions by zero (certain operations have no return-value means of reporting failure ). using exception handling for API and logical failure reporting is beneficial in that it unifies error handling for all failure types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.