ITWissen.info - Tech know how online

exception

An exception is a class oferrors that occur during runtime. If no software measures are taken for these cases, this often leads to a crash of the entire software system. But also other unusual situations can be caught and even solved by a specific exception handling, so that the program can be executed afterwards.

The conceptual basis of exception handling takes into account that a situation in a low- level function generates an exception, this is passed on through the calling levels and is finally handled centrally by a piece of program code - the so-called exception handler. For exception handling, most programming languages such as C++, C#, Java, Eiffel, Ada, Python, Ruby, Delphi, or Scala, among others, provide appropriate mechanisms by default.

In a program or the runtime environment, such as the Java Runtime Environment ( JRE), situations can occur during runtime that are generally referred to as runtime errors and lead to an exception. These situations can have various causes, such as the entry of invalid values, an invalid division by zero, an unstable network connection, the printer running out of paper for a given print job, or the opening of a file that does not exist.

All these situations have one thing in common: If the error is not handled, they lead to the termination of the program. Situations of this kind are called exceptions, and must be recognized and handled during software development. The objective of this error handling is to enable the user of a program to continue the program, for example, by correcting his input, or, in extreme cases, to terminate the program after saving all necessary data. This form of error handling is also known as Structured Exception Handling.

Runtime Errors and Error Objects

When a runtime error occurs, it generates an error object that encapsulates the details of the error that occurred. This object searches within the runtime environment for an exception handler, which in turn receives the error object and takes care of handling it according to the requirements of the respective application. The exception can either be handled directly by the causal part of the program or forwarded. The recipient of the error object can then also either handle it or forward it to the level above. If an exception is not handled by any of the called methods, the software terminates in an undefined state

The different programming languages support similar concepts for exception handling. For example, in Delphi, an exception can be handled as shown

Exception in Delphi

Exception in Delphi

The statements that can potentially cause an exception are isolated in a try block. Unless a runtime error occurs, the statements in the try block are executed as defined, and the program then continues past the expect block. If one of the statements in the try block causes a runtime error, the try block is terminated immediately and the statements in the expect block are executed first, before the defined program flow is resumed.

In the following, the concepts for exception handling with exceptions are explained in more detail using the Java programming language as an example, since this provides a good general understanding of the possibilities of exception handling.

An exception can be raised by the runtime environment or software-controlled by the developer of the software. At certain points in the software, the compiler expects that an exception handling is explicitly programmed. The throwing of an exception is called "throwing" and its handling is called "catching". Exceptions are only thrown within a method. Java provides the following constructs related to exceptions:

try. defines a block within which exceptions can occur.

catch Defines a block that performs exception handling.

finally Defines a block that performs final operations.

throw Generates an exception under software control.

throws Declares which exception can be thrown by a method.

Example of Java constructs with exceptions

Example of Java constructs with exceptions

In this example, the compiler determines whether handling has occurred for the NumberFormatException exception: by including the exceptions in the method header, the compiler verifies that all exceptions are handled. This is different in Java than in C++, for example.

In Java, a hierarchy of classes is implemented for exceptions, and an exception is always an object of these classes. The figure illustrates the structure of this hierarchy. Here, exceptions generated directly by the Java Virtual Machine ( JVM), for example OutOfMemoryExpection or NullPointerExpection, are handled by the superclass of all error classes Throwable. Exceptions are divided into controlled and uncontrolled exceptions, where uncontrolled exceptions do not need to be caught by the software developer. The exceptions of type Error and RuntimeException shown in the figure are uncontrolled exceptions, all other exceptions are controlled exceptions, which have to be handled by the software developer, otherwise the compiler already reports an error. The cause for exceptions of the type Error are causally based in the JVM, while RuntimeException can be excluded by careful programming. The Java documentation specifies - as shown in the above example with regard to the method parseint() - which exception a method can throw.

The concept of Java provides for the definition of own exception classes. In doing so, one derives a class from Exception or a subclass of Exception, which can then be used as well as all predefined exception classes:

Class MyException extends Exception {};

Java, for example, has the ability to pass on exceptions in a program according to a defined mechanism. In this case, the passing method tells the compiler that the exception may occur but will not be caught by that method.

Class hierarchy of exceptions

Class hierarchy of exceptions

An interesting method is getStackTrace(), which belongs to the superclass Throwable. This allows access to elements of the stack, which makes a differentiated analysis of errors possible, since the sequence of calls of all methods up to the exception is made available on the standard console. The getMessage() and toString() methods of Throwable provide further information on exceptions thrown. GetMessage() returns a detailed message string for the exception, and toString() also returns the class name.

Informations:
Englisch: exception
Updated at: 28.10.2013
#Words: 990
Links: class, software (SW), crash, system, program
Translations: DE
Sharing:    

All rights reserved DATACOM Buchverlag GmbH © 2024