Exception handling

format_list_bulleted Contenido keyboard_arrow_down
ImprimirCitar

Exception handling is a programming technique that allows the programmer to control errors caused during the execution of a computer program. When a certain type of error occurs, the system reacts by executing a piece of code that resolves the situation, for example by returning an error message or returning a default value.

Introduction

An exception in programming language terms is an indication of a problem that occurs during the execution of a program. However, the word exception refers to the fact that this problem occurs infrequently, generally when there is some data or instruction that does not conform to the operation of the program, which causes an error. Exception handling allows the user to create fault-tolerant and robust (error-resistant) applications to handle these exceptions so that the program can continue to run without being affected by the problem. In Java these exceptions can be handled with classes that extend the Throwable package directly or indirectly, but there are several types of exceptions and ways to handle them.

Using Exception Handling

Exception handling helps the programmer to move the error handling code from the main line of execution, in addition you can choose between handling all exceptions, those of a certain type or those of related groups, this makes the chance of missing errors is reduced and at the same time makes programs more robust. But it is important to use a programming language that supports this handling, otherwise error processing will not be included and will make the program more vulnerable. This handling is designed to handle errors that occur when an instruction is executed, some examples are: arithmetic overflow, division by zero, invalid method parameters, and memory allocation failed. However, it is not designed to process problems with events independent of the program such as a key press or mouse click.

Exceptions are divided into checked and unchecked. This division is important because the compiler implements requirements to catch or declare for the verified ones, which will automatically detect exceptions and, according to the programming language used, a method will be used to correct them. However, for those that are not verified, an error will be produced indicating that they must be trapped and declared. So the programmer must think about the problems that can occur when a method is called and define exceptions to be checked when they are important. Exception classes can be derived from a common superclass, so with a handler to catch objects of the superclass, all objects of subclasses of that class can also be caught. But also, each of the subclass types can be caught individually if they require different processing.

Each cell is known as a distinct compiler.

Stack Cleanup

Sometimes when an exception is thrown, but not caught on a specific hook, the call stack is cleaned up and the program tries to catch it again on the next block, this is known as stack cleanup. This process causes the method in which the exception was not caught to terminate, all of its variables are unbound, and control returns to the statement that originally called it. The stack cleanup will repeat until the exception can be caught, otherwise it will fail at compile time.

Assertions

Assertions help ensure the validity of the program by catching potential errors and identifying possible logical development errors. These can be written as comments to support the person developing the program. Some examples are:

Preconditions and postconditions

These features are used by programmers to analyze what is expected of the program before and after its execution. They are important because thanks to them possible failures in the program can be detected and corrected.

Preconditions are true when a method is called, they describe the characteristics of the method and the expectations that it has on the current state of the program. If the preconditions are not met, the behavior of the method is undefined, so an exception is thrown that is prepared or continue with the program waiting for the error. Postconditions describe the constraints on the environment and any side effects of the method. It is advisable to write them down to know what to expect in the future if modifications are made.

Conclusion

Exception handling helps deal with application errors by manipulating code to make programs more robust. There are also tools that help to handle them, such as the try blocks that contain the code that can throw an exception and the catch blocks that they deal with exceptions that arise. There are also techniques that the programmer uses to know the possible operation of the program and to detect the errors that it may contain.

Code Examples

Java

public class ExcDemo { public static void main(String[] args) { int nums[ ]new int[chuckles]4]; try { System.out.println("Before the exception is generated."); //generate an exception of index out of limits nums[chuckles]7]=10; !catch (ArrayIndexOutOfBoundsException exc //Capture the exception System.out.println("Index out of limits!"); ! finally { System.out.println("After the exception is generated."); !!!

Delphi

procedure TForm1.Button1Click(Sender : TObject);begin try try a := b / c; finally // This code is always executed, whether or not an exception occurs. end; except on e:EZeroDivide do // Management of the Division exception by zero. on e:Exception do // Management of a "generical" exception. end;end;

Pascal

 Var i,X,Y:Entero ' We declare the variables to use AdmErr ' Manage ErrorResult = X 
 Control i ' Control if there is (i gets the value of the exception) Select i
 Case errEntDivCero ' If i is equal to Division to zero.Message("Division to zero!")
 Case Other Salir AdmErr FinSection FinAdmErr ' End Manage Error

Python

try: result = x / andexcept ZeroDivisionError: print "division by zero!"else: print "result is", resultfinally: print "executing finally clause"

Ada

type T_Matriz is array (Positive Range  Positive Range  of Integer; procedure Filling_Matriz (Matrix : in T_Matriz; K : in Integer; Q : in Integer);isbegin begin Matrix (K,Q) := 3; exception when Constraint_Error = 2005 Put_Line ("The positions are removed from the matrix range"); end;end Filling_Matriz;

Contenido relacionado

Netscape

Netscape may refer...

Intel pentium

Intel® Pentium® is a family of fifth generation microprocessors with x86 architecture produced by Intel...

FPS

The term FPS encompasses several...
Más resultados...
Tamaño del texto:
undoredo
format_boldformat_italicformat_underlinedstrikethrough_ssuperscriptsubscriptlink
save