How to Deal With Errors in C++

As a programmer, it's your job to produce robust systems that run smoothly at all times. You don't have to write functions that check flags and return cryptic error codes. Program recovery from a bad situation can be smooth because of a mechanism called C++ Exception Handling. Objects called Exceptions can be "thrown" from the error site and "caught" by an Exception Handler, giving you a chance to set things right.

1.Create an exception class for each base class in your design using a C++ development environment. All can share a common structure. For a File base class create a FileException exception class, for a GameEntity base class create a GameEntityException exception class. Get the pattern?

2.Place the exception class inside the public area of the base class. The exception class has to be visible as it will be called by the handler.

3.Give each exception class a constructor that accepts an input string. The input string will contain the error message that will be displayed when the exception object gets caught by the handler.

4.Create a handler function for each exception class you have designed. Each handler function is an overloaded "catch" function differing in the type of input argument. The first line of the two handlers would look like, "catch(File::FileException* e) {…" and "catch(GameEntity::GameEntityException* e) {…". Place these exception handler functions right underneath 'main.'

5.Inundate your programming space with "throw" statements. Put them in two major areas. Insert "throw" statements where you see potential for trouble, "if (/*divide by zero*/) {throw Math::MathException("division by zero");}. Declare a 'throw' statement on the right of a function or class that calls exceptions, "void Divide(/*parameters*/) throw (Math::MathException) {…".

6.Insert your execution code inside "main," within "try" braces. The C++ exception handling mechanism has a try, throw and catch structure. Not only does this make programs extremely robust but it is pleasant to read

Hiç yorum yok:

2018 SGK İşten Çıkış Kodları (696 sayılı KHK kapsamında eklenenler dahil)

2018 SGK İşten Çıkış Kodları 1- Deneme süreli iş sözleşmesinin işverence feshi 2- Deneme süreli iş sözleşmesinin işçi tarafından feshi ...