# CorruptionHandler

Artifact: [androidx.datastore:datastore-core](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/core/CorruptionHandler.kt+class:androidx.datastore.core.CorruptionHandler) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.2.0)

*** ** * ** ***


```
public interface CorruptionHandler<T extends Object>
```

<br />

Known direct subclasses [ReThrowCorruptionHandler](https://developer.android.com/reference/androidx/datastore/core/handlers/ReThrowCorruptionHandler), [ReplaceFileCorruptionHandler](https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler)

|---|---|
| `https://developer.android.com/reference/androidx/datastore/core/handlers/ReThrowCorruptionHandler` | Default corruption handler which does nothing but rethrow the exception. |
| `https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler` | A corruption handler that attempts to replace the on-disk data with data from produceNewData. |

*** ** * ** ***

CorruptionHandlers allow recovery from corruption that prevents reading data from the file (as indicated by a CorruptionException).

## Summary

| ### Public methods |
|---|---|
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull T` | `https://developer.android.com/reference/androidx/datastore/core/CorruptionHandler#handleCorruption(androidx.datastore.core.CorruptionException)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/CorruptionException ex)` This function will be called by DataStore when it encounters corruption. |

## Public methods

### handleCorruption

```
abstract @NonNull T handleCorruption(@NonNull CorruptionException ex)
```

This function will be called by DataStore when it encounters corruption. If the implementation of this function throws an exception, it will be propagated to the original call to DataStore. Otherwise, the returned data will be written to disk.

This function should not interact with any DataStore API - doing so can result in a deadlock.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/CorruptionException ex` | is the exception encountered when attempting to deserialize data from disk. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | The value that DataStore should attempt to write to disk. |