﻿# Lesson 20\. Pattern 12\. Exceptions

Generation and processing of exceptions using integer types is a bad practice of C\+\+ programming\. You should use more informative types for these purposes, for example types derived from the class _std::exception_\. But sometimes you have to deal with a low\-quality code like this:

```cpp
char *ptr1;
char *ptr2;
try {
  try {
    throw ptr2 - ptr1;
  }
  catch (int) {
    std::cout << "catch 1: on x86" << std::endl;
  }
}
catch (ptrdiff_t) {
  std::cout << "catch 2: on x64" << std::endl;
}
```

You should be very attentive and avoid generation or processing of exceptions using [memsize](https://pvs-studio.com/en/blog/terms/0030/)\-types because it may result in changes of program logic\. To correct this code you may replace "catch \(int\)" with "catch \(ptrdiff\_t\)"\. A more correct way is to use a special class to pass the information about an error that has occurred\.

## Diagnosis

We have not encountered errors of this type in practice yet but the tool [PVS\-Studio](https://pvs-studio.com/en/pvs-studio/) can detect them\. The diagnostic message [V115](https://pvs-studio.com/en/docs/warnings/v115/) will be shown when an exception is generated with the help of a memsize\-type, while the warning [V116](https://pvs-studio.com/en/docs/warnings/v116/) will be generated when a memsize\-type is used in _catch_ operator\.

_The course authors: Andrey Karpov \([karpov@viva64\.com](mailto:karpov@viva64.com)\), Evgeniy Ryzhkov \([evg@viva64\.com](mailto:evg@viva64.com)\)\._

_The rightholder of the course "Lessons on development of 64\-bit C/C\+\+ applications" is OOO "Program Verification Systems"\. The company develops software in the sphere of source program code analysis\. [The company's site](https://pvs-studio.com/en/)\._