answersLogoWhite

0


Best Answer

C does not provide native support for exception handling.

There are basically three error handling strategies in C. The first is to design all functions such that the return value indicates success or failure. The second is to use an output parameter to indicate success or failure. The third is to set a global variable to indicate success or failure.

Integer values are usually used to indicate success or failure. Typically, the all-zeros bit-pattern indicates success while the all-ones bit pattern indicates failure (equating to 0 and -1, respectively, on a twos-complement system). However, any non-zero value can be used to indicate an error because all non-zero values implicitly convert to true (an error has occurred). Thus the error code can be used to indicate a specific type of error.

The problem with this is that there is often a lot of code between the point where an error is detected and the point where it can be handled. With global error codes this can be extremely problematic (particularly in multi-threaded applications) because the error code could easily change if other errors occur en-route to the error-handling code. But even for "local" return values and output parameters, this usually means storing the error code somewhere that the error-handling code can easily retrieve it. The only alternative is to deal with every error as and when it occurs, which is often impractical, but also leads to error handling code being duplicated and widely-dispersed.

In C++ we do not have these problems. If we invoke a C library function that returns or sets an error code that cannot be handled there and then, we can simply convert that code into an exception object and throw the object for a suitable handler to deal with. Once an exception is thrown by a thread, the call stack unwinds, immediately terminating functions (without returning a value) until a suitable handler is found. Unhandled exceptions that make it all the way back to the main function can be caught with a catch-all exception handler. By making consistent use of resource handles and "smart" pointer objects rather than "naked" pointers, all resources consumed between the error handling code and the point where the error was initially detected are automatically released, thus minimising the amount of manual housekeeping operations required en-route to the exception handling code. This technique is known by the unwieldy term Resource Acquisition Is Initialisation (RAII) but all this means is that named objects that utilise RAII will release their acquired resources automatically when they fall from scope.

As with exception-handling, RAII is not supported in C because objects in C have no constructors or destructors. When a "naked" pointer falls from scope, the object it was pointing at is not automatically released; it is up to the programmer to manually release the resource at the appropriate time. With multiple pointers referring to the same object (a shared resource), it can be difficult to determine which pointer actually "owns" the resource. With smart pointers we do not have this problem.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which data types can be thrown as exceptions in a c program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the hierarchy of the java program?

define the data types


What is primitive and composite data types in DBMS?

a composite data type is any data type which can be constructed in a program using its programming language's primitive data types and other composite types. The act of constructing a composite type is known as composition.


How do you find out the size of all the data types in C program?

sizeof is your friend.


What is program data dependence?

Program data dependence refers to the coupling of data stored in files and the specific programs required to update and maintain those files such that changes in programs require changes to the data. In a tradition file environment, any change in a software program could require a change in the data accessed by that program.


What has the author Daniel F Stubbs written?

Daniel F. Stubbs has written: 'Data structures with abstract data types and Ada' -- subject(s): Abstract data types (Computer science), Ada (Computer program language), Data structures (Computer science)


Which header file needs to be included in a program that uses the data types ifsteam and ofsteam?

#include <fstream>


Why are data translation utilities of use?

Some programs can only use certain file types. Data translation changes the file type to a type that is usable by the program.


What has the author Reinhold Friedrich Hille written?

Reinhold Friedrich Hille has written: 'Data Abstraction and Program Development Using Modula Two' 'Data abstraction and program development using Pascal' -- subject(s): Abstract data types (Computer science), Computer programming


What is meant by data and its types?

A data type of an object describes the kind of information stored in that object. Numbers, strings, images, and all other information in a program is described by some sort of data type.


Is built-in data-type are abstract data-types in java?

All built-in data types are not abstract data types.


What is the difference between data and and program?

A program is a type of data.A program in a computer is group of computer instructions or commands that, when loaded into memory, can be run by the computer processor to accomplish a task. Data is information that can be used by a computer program. It can be data in memory as part of the "workings" of a program, it can be stored in an area called a file or database from where it can be read and written.In modern computers, both data and program can exist in the same memory areas and on the same storage media. Programs are first made in a human readable text form and treated as data while it is being created and modified/edited. Another program (or programs) will convert this data into another form of data which can serve as a computer program.On most computer systems, program and data are stored as files. Usually a program is distinguished from other data by its name (its name will end with ".EXE") or by a special information designating the file as "executable". This is different on the IBM i platform (and its predecessors) where a program is maintained as a program object (*PGM) and data is stored as various types of data and file objects (*FILE).


What is the difference between data and program?

A program is a type of data.A program in a computer is group of computer instructions or commands that, when loaded into memory, can be run by the computer processor to accomplish a task. Data is information that can be used by a computer program. It can be data in memory as part of the "workings" of a program, it can be stored in an area called a file or database from where it can be read and written.In modern computers, both data and program can exist in the same memory areas and on the same storage media. Programs are first made in a human readable text form and treated as data while it is being created and modified/edited. Another program (or programs) will convert this data into another form of data which can serve as a computer program.On most computer systems, program and data are stored as files. Usually a program is distinguished from other data by its name (its name will end with ".EXE") or by a special information designating the file as "executable". This is different on the IBM i platform (and its predecessors) where a program is maintained as a program object (*PGM) and data is stored as various types of data and file objects (*FILE).