# ReplaceFileCorruptionHandler

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/handlers/ReplaceFileCorruptionHandler.+class:androidx.datastore.core.handlers.ReplaceFileCorruptionHandler) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  class ReplaceFileCorruptionHandler<T : Any?> : CorruptionHandler<T>
  ```

*** ** * ** ***

A corruption handler that attempts to replace the on-disk data with data from produceNewData.

If the handler successfully replaces the data, the original exception is swallowed. If the handler encounters an exception when attempting to replace data, the new exception is added as a suppressed exception to the original exception and the original exception is thrown.

## Summary

| ### Public constructors |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler#ReplaceFileCorruptionHandler(kotlin.Function1)( produceNewData: (https://developer.android.com/reference/kotlin/androidx/datastore/core/CorruptionException) -> T )` | Cmn android N JS |

| ### Public functions |
|---|---|---|
| `open suspend T` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler#handleCorruption(androidx.datastore.core.CorruptionException)(ex: https://developer.android.com/reference/kotlin/androidx/datastore/core/CorruptionException)` This function will be called by DataStore when it encounters corruption. | Cmn android N JS |

## Public constructors

### ReplaceFileCorruptionHandler

Cmn android N JS

```
<T : Any?> ReplaceFileCorruptionHandler(
    produceNewData: (CorruptionException) -> T
)
```

## Public functions

### handleCorruption

Cmn android N JS

```
open suspend fun handleCorruption(ex: CorruptionException): T
```

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 |
|---|---|
| `ex: https://developer.android.com/reference/kotlin/androidx/datastore/core/CorruptionException` | is the exception encountered when attempting to deserialize data from disk. |

| Returns |
|---|---|
| `T` | The value that DataStore should attempt to write to disk. |