Home  >  Article  >  Java  >  Exception mechanism of programming language

Exception mechanism of programming language

零下一度
零下一度Original
2017-07-27 17:04:211549browse

Overview

Exception handling is a mechanism in a programming language or computer hardware that is used to handle abnormal conditions that occur in software or information systems (that is, beyond the normal execution flow of the program) certain special conditions). Various programming languages ​​have very significant differences in handling exceptions (the difference between error detection and exception handling is: error detection is code that handles unforeseen problems in the normal program flow, such as a call operation that fails to end successfully) . Some programming languages ​​have functions that cannot be safely called when the input contains illegal data, or the return value cannot be effectively distinguished from exceptions. For example, the atoi function in C language (ASCII string to integer conversion) can return 0 when the input is illegal. In this case, the programmer needs to perform additional error detection (perhaps through some auxiliary global variables such as C's errno), or input verification (such as through regular expressions), or use a combination of both methods.

Through exception handling, we can control and prompt users for illegal input in the program to prevent the program from crashing.

From a process perspective, hardware interrupts are equivalent to recoverable exceptions, although interrupts generally have nothing to do with the program flow itself.

From the perspective of a subprogram programmer, exceptions are a very useful mechanism for notifying the outside world that the subprogram cannot execute normally. If the entered data is invalid (for example, the divisor is 0), or the required resources are not available (for example, the file is missing). If the system does not have an exception mechanism, programmers need to use return values ​​to indicate what errors occurred. [1]

Exception mechanism of programming languages

Many common programming languages, including Actionscript, Ada, BlitzMax, C++, C#, D, ECMAScript, Eiffel, Java, ML, Object Pascal (such as Delphi, Free Pascal, etc.), Objective-C, Ocaml, PHP (version 5), PL/1, Prolog, Python, REALbasic, Ruby, Visual Prolog and most .NET programming languages, built-in exception mechanism They all search backward along the function call stack until they encounter the exception handling code. Generally, stack unwinding is completed step by step during the search process of this exception handling code. But Common Lisp is an exception. It does not use stack rollback, so it allows execution to resume where the exception was thrown after the exception is handled. And Visual Basic (especially in its versions earlier than .net, such as 6.0) goes further: the on error statement can easily specify whether to retry (resume), skip (resume next), or execute the programmer after an exception occurs. Defined error handler (goto ***). [1]

The syntax of the exception mechanism in most languages ​​is similar: use throw or raise to throw an exception object (Java or C++, etc.) or a special extensible enumeration type value (such as Ada language ); the scope of the exception handling code is marked with a mark clause (language scope starting with try or begin) to mark its start, and the first exception handling clause (catch, except, resume, etc.) is used to mark its end; it can appear continuously Several exception handling clauses, each handling a specific type of exception. Some languages ​​allow else clauses for situations where no exceptions occur. More common is the finally, ensure clause, which will be executed regardless of whether an exception occurs, and is used to release some resources required for exception handling. [1]

C++ exception handling is the basis of Resource-Acquisition-Is-Initialization. [1]

C language is generally considered not to support exception handling. The Perl language optionally supports structured exception handling. [1]

The Python language is very general and in-depth about the exception handling mechanism, so it is very difficult to write a program without try and except. [1]


Explanation of terms

Exception handling, the English name is exceptional handling, is a new method that replaces the declining error code method and provides all the error code methods. Unable to specify advantages. Exception handling separates receiving and handling error codes. This function clarifies the thoughts of programmers and helps enhance the readability of the code, making it easier for maintainers to read and understand.

Exception handling (also known as error handling) function provides a way to handle any unexpected or abnormal conditions that occur while the program is running. Exception handling uses the try, catch, and finally keywords to attempt operations that may not succeed, handle failures, and clean up resources afterward.

Exception handling is usually a measure taken to prevent unknown errors from occurring. The advantage of exception handling is that you no longer have to rack your brains to consider various errors. This provides a very effective method for handling certain types of errors, greatly improving programming efficiency.

Exceptions can be generated by the common language runtime (CLR), third-party libraries, or application code using the throw keyword.


Features

1. When the application encounters an abnormal situation (such as division by zero or insufficient memory warning), an exception will be generated.

2. When an exception occurs, the control flow immediately jumps to the associated exception handler (if it exists).

3. If there is no exception handler for the given exception, the program will stop execution and display an error message.

4. Operations that may cause exceptions are performed using the try keyword.

5.Exception handler is a block of code that is executed when an exception occurs. In C#, the catch keyword is used to define exception handlers.

6. The program can use the throw keyword to explicitly throw an exception.

7. The exception object contains detailed information about the error, including the state of the call stack and a text description of the error.

8. Even if an exception is thrown, the code in the finally block will be executed, allowing the program to release resources.



The above is the detailed content of Exception mechanism of programming language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn