# Error Handling

Error handling is an integral part of software development. This article provides effective ways to handle LicenseSpring errors and keep your application secure.

### Prerequisites

{% stepper %}
{% step %}

### Completed the Getting Started tutorial

* Follow the [**Getting Started**](https://docs.licensespring.com/sdks/tutorials/best-practices/broken-reference) Tutorial, specifically:
* Follow the [**Getting Started**](https://docs.licensespring.com/sdks/tutorials/getting-started) tutorial, specifically:
  * Initialized `LicenseManager` (or `LicenseHandler`) with your configuration using the appropriate settings.
  * Created a `LicenseID` using either `LicenseID::fromKey` or `LicenseID::fromUser` function, depending on the activation method you prefer.
  * Implemented basic license management, including license activations, deactivations, and both online and local license checks.
    {% endstep %}
    {% endstepper %}

### Handling Exceptions

Methods of License and LicenseManager classes throw exceptions if something went wrong. Put the methods that can produce exceptions in `try...catch` block.

`LicenseSpringException` is the base class of all SDK exceptions. The good practice is to implement separate logic for different types of exceptions. Below is the example of handling device id mismatch. Instead of closing the application, we can try to upgrade to a new device id algorithm. See: [**Hardware ID generation**](https://docs.licensespring.com/sdks/tutorials/best-practices/hardware-id-generation)

{% tabs %}
{% tab title="C++" %}

```cpp
try
{
    license->localCheck();
}
catch( const DeviceNotLicensedException& ex )
{
    // Handling specific error: trying to upgrade to new device id algorithm
    license = m_licenseManager->relinkLicense( WinCryptographyId );
    return;
}
catch( const LicenseSpringException& ex )
{
    std::cout << "LicenseSpring exception encountered: " << ex.what();
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
try
{
    license.LocalCheck();
}
catch( DeviceNotLicensedException ex )
{
    // Handling specific error: trying to upgrade to new device id algorithm
    license = manager.RelinkLicense( DeviceIDAlgorithm.Gen3 );
    return;
}
catch (LicenseSpringException ex )
{
    Console.WriteLine( "LicenseSpring exception encountered: {0}", ex.Message );
}
```

{% endtab %}

{% tab title="Java" %}

```java
try {
    license.LocalCheck();
} catch(LicenseExpiredException e) {
    // Handling specific error
} catch (LicenseSpringException ex ) {
    System.out.println("LicenseSpring exception encountered: " + ex.getMessage());
}
```

{% endtab %}
{% endtabs %}

#### Common Exceptions

Below is the list of the exceptions that can occur on any SDK request:

| Exception                                                                                                              | Definition                                                                                                                                                                                                  |
| ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `LicenseStateException`                                                                                                | License is disabled, was reset or expired                                                                                                                                                                   |
| `SignatureMismatchException`                                                                                           | In case signature returned by LicenseSpring server is not valid. E.g. if the response data was changed.                                                                                                     |
| <p><code>NoInternetException</code><br><code>NetworkTimeoutException</code><br><code>LicenseServerException</code></p> | Connection-related errors: if there's no Internet or an internal server error occured. See [**License Checks (Grace Period)**](https://docs.licensespring.com/sdks/tutorials/best-practices/license-checks) |
| `ApiKeyException`                                                                                                      | If provided api key cannot be used for the request( is invalid, readonly or revoked).                                                                                                                       |
| `AuthorizationException`                                                                                               | Authorization failed, please check your proxy settings.                                                                                                                                                     |

### Handling C++ Errors

The `LicenseHandler` class of C++ SDK is exception-free by default. It provides an alternative way to implement licensing without exceptions but C-style error codes. Make sure to put error checks after `LicenseHandler` calls.

To retrieve the error use the following methods:

```cpp
bool LicenseHandler::wasError() const; //check if there was an error
LSErrorCode LicenseHandler::getLastError() const; //get code of the last error
const std::string& LicenseHandler::getLastErrorMsg() const;//get message of the last error
```

This class also contains several methods to check for error type, e.g. `isLicenseStateError`, `isNoInternetError`.

Example of handling `LicenseHandler` errors:

```cpp
licenseHandler.checkLicenseLocal();
if( licenseHandler.wasError() )
{
    if( licenseHandler.getLastError() == eDeviceError )
        licenseHandler.relinkLicense( WinCryptographyId );
    else
        std::cout << "LicenseSpring error encountered: " << licenseHandler.getLastErrorMsg();
}
```

#### Common Errors

Below is the list of the exceptions that can occur on any SDK request:

| Error                                                                                                                                      | Definition                                                                                                                                                                                                  |
| ------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `eStdError`                                                                                                                                | C++ standard exception encountered                                                                                                                                                                          |
| `eSignatureMismatchError`                                                                                                                  | Server signature is not valid. E.g. if the response data was changed.                                                                                                                                       |
| <p><code>eNoInternetError</code><br><code>eNetworkTimeoutError</code><br><code>eServerError</code></p>                                     | Connection-related errors: if there's no Internet or an internal server error occured. See [**License Checks (Grace Period)**](https://docs.licensespring.com/sdks/tutorials/best-practices/license-checks) |
| <p><code>eInvalidApiKey</code><br><code>eReadOnlyApiKey</code><br><code>eRevokedApiKey</code><br><code>eApiKeyProductNotAllowed</code></p> | If provided api key cannot be used for the request( is invalid, readonly or revoked).                                                                                                                       |
| `eAuthorizationError`                                                                                                                      | Authorization failed, please check your proxy settings.                                                                                                                                                     |

### Handling Swift errors

Swift SDK uses `LSError` compatible with `NSError` for error reporting.

Any SDK method marked as `throws` produces `LSError` in case of error. Possible error codes described as `enum LSErrorCode`.

{% tabs %}
{% tab title="Swift" %}

```swift
do {
    try license.fullCheck()
} catch let error as LSError {
    print("LicenseSpring error: \(error)")
} catch {
    print("Unknown error: \(error)")
}
```

{% endtab %}
{% endtabs %}
