# C++: Statements: try...catch

## Syntax

       try {
          // normal statements
       } catch (some-exception-type e) {
          // process exception
       }

## Exceptions are thrown, you can catch them

When your program causes an error in some library functions, an exception is created and thrown. The system looks for enclosing `try...catch` statement that can handle (process) this exception. First it looks to see if the code that caused the exception is inside of a `try` statement that can handle the exception.
If the `try` statement has a `catch` part that can handle this type of exception, then it goes to that code.
If there is no `try` statement around the code that caused the error (exception), the system looks at the function that called this function, and checks for a `try` statement there. It continues unwinding the call stack looking for someone who can handle the exception. Ultimately, the system function that called `main` will handle it if no one else does.

## Kinds of Exceptions

There are many kinds of exceptions, but they can be generally put into two groups:

- Programming errors -- There is something wrong with your program. For example, you should never get an `out_of_range` exception. If you do, there is something wrong with your program, and you need to fix it. You never need to catch exceptions from programming errors.
- Input errors -- These are usually errors in the input data. The programmer has no control over the input the user gives you, eg, file names, integer values, .... If the user gives you a bad value, it may cause an exception when you use it. You need to check for bad input using a `try` statement.

## `try` statement -- What you can do to catch Errors

You need to put a `try` statement around any section of code that might generate a user generated error.

## Example

[need example]

## Advanced

You can also use exceptions of your own, and generate them using the `throw` statement.