

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Menangani kesalahan dalam AWS SDK untuk PHP Versi 3
<a name="handling-errors"></a>

## Penanganan Kesalahan Sinkron
<a name="synchronous-error-handling"></a>

Jika terjadi kesalahan saat melakukan operasi, pengecualian dilemparkan. Untuk alasan ini, jika Anda perlu menangani kesalahan dalam kode Anda, gunakan`try`/`catch`blok di sekitar operasi Anda. SDK melempar pengecualian khusus layanan saat terjadi kesalahan.

Contoh berikut menggunakan`Aws\S3\S3Client`. Jika ada kesalahan, pengecualian yang dilemparkan akan menjadi tipe`Aws\S3\Exception\S3Exception`. Semua pengecualian khusus layanan yang dilemparkan SDK diperluas dari kelas. `Aws\Exception\AwsException` Kelas ini berisi informasi yang berguna tentang kegagalan, termasuk request-id, kode kesalahan, dan jenis kesalahan. Catatan untuk beberapa layanan yang mendukungnya, data respons dipaksa menjadi struktur array asosiatif (mirip dengan `Aws\Result` objek), yang dapat diakses seperti array asosiatif PHP normal. `toArray()`Metode ini akan mengembalikan data tersebut, jika ada.

 **Impor** 

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

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

 **Kode Sampel** 

```
// 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());
}
```

## Penanganan kesalahan asinkron
<a name="asynchronous-error-handling"></a>

Pengecualian tidak dilemparkan saat mengirim permintaan asinkron. Sebagai gantinya, Anda harus menggunakan `then()` atau `otherwise()` metode janji yang dikembalikan untuk menerima hasil atau kesalahan.

 **Impor** 

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

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

 **Kode Sampel** 

```
//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);
});
```

Anda dapat “membuka” janji dan menyebabkan pengecualian dilemparkan sebagai gantinya.

 **Impor** 

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

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

 **Kode Sampel** 

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

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