

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Gestione degli errori nella AWS SDK per PHP versione 3
<a name="handling-errors"></a>

## Gestione degli errori sincroni
<a name="synchronous-error-handling"></a>

Se si verifica un errore durante l'esecuzione di un'operazione, viene generata un'eccezione. Per questo motivo, se è necessario gestire gli errori nel codice, utilizza blocchi `try`/`catch` intorno alle operazioni. L'SDK genera eccezioni specifiche per il servizio quando si verifica un errore.

Gli esempi seguenti utilizzano `Aws\S3\S3Client`. Se si verifica un errore, l'eccezione generata sarà del tipo `Aws\S3\Exception\S3Exception`. Tutte le eccezioni specifiche per il servizio generate dall'SDK si estendono dalla classe `Aws\Exception\AwsException`. Questa classe contiene informazioni utili sull'errore, tra cui l'id della richiesta, il codice errore e il tipo di errore. Nota per alcuni servizi che la supportano, i dati di risposta vengono convertiti in una struttura array associativa (simile agli oggetti `Aws\Result`), a cui è possibile accedere come un normale array associativo PHP. Il metodo `toArray()` restituirà tali dati, se esistenti.

 **Importazioni** 

```
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
```

 **Codice di esempio** 

```
// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk([
    'region'   => 'us-west-2'
]);

// Use an Aws\Sdk class to create the S3Client object.
$s3Client = $sdk->createS3();

try {
    $s3Client->createBucket(['Bucket' => 'amzn-s3-demo-bucket']);
} catch (S3Exception $e) {
    // Catch an S3 specific exception.
    echo $e->getMessage();
} catch (AwsException $e) {
    // This catches the more generic AwsException. You can grab information
    // from the exception using methods of the exception object.
    echo $e->getAwsRequestId() . "\n";
    echo $e->getAwsErrorType() . "\n";
    echo $e->getAwsErrorCode() . "\n";

    // This dumps any modeled response data, if supported by the service
    // Specific members can be accessed directly (e.g. $e['MemberName'])
    var_dump($e->toArray());
}
```

## Gestione asincrona degli errori
<a name="asynchronous-error-handling"></a>

Le eccezioni non vengono generate durante l'invio di richieste asincrone. Al contrario, è necessario utilizzare il metodo `then()` o `otherwise()` della promessa restituita per ricevere il risultato o l'errore.

 **Importazioni** 

```
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
```

 **Codice di esempio** 

```
//Asynchronous Error Handling
$promise = $s3Client->createBucketAsync(['Bucket' => 'amzn-s3-demo-bucket']);
$promise->otherwise(function ($reason) {
    var_dump($reason);
});

// This does the same thing as the "otherwise" function.
$promise->then(null, function ($reason) {
    var_dump($reason);
});
```

È possibile "aprire" la promessa e causare invece la creazione dell'eccezione.

 **Importazioni** 

```
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
```

 **Codice di esempio** 

```
$promise = $s3Client->createBucketAsync(['Bucket' => 'amzn-s3-demo-bucket']);
```

```
//throw exception
try {
    $result = $promise->wait();
} catch (S3Exception $e) {
    echo $e->getMessage();
}
```