다음을 통해 공유


Async.Catch<'T> 메서드(F#)

업데이트: 2010년 8월

지정한 계산을 실행하는 비동기 계산을 만듭니다. 이 계산이 성공적으로 완료되면 이 메서드는 반환된 값과 함께 Choice1Of2를 반환합니다. 이 계산이 완료되기 전에 예외가 발생하면 발생한 예외와 함께 Choice2Of2를 반환합니다.

네임스페이스/모듈 경로: Microsoft.FSharp.Control

어셈블리: FSharp.Core(FSharp.Core.dll)

// Signature:
static member Catch : Async<'T> -> Async<Choice<'T,exn>>

// Usage:
Async.Catch (computation)

매개 변수

  • computation
    형식: Async<'T>

    'T 형식을 반환하는 입력 계산입니다.

반환 값

'T 형식의 Choice 또는 예외를 반환하는 계산입니다.

예제

다음 코드 예제에서는 Async.Catch를 사용하여 예외를 throw할 수 있는 비동기 계산을 실행하는 방법에 대해 설명합니다.

open System
open System.IO

let writeToFile filename numBytes = 
    async {
        use file = File.Create(filename)
        printfn "Writing to file %s." filename
        do! file.AsyncWrite(Array.zeroCreate<byte> numBytes)
    }

let readFile filename numBytes =
    async {
        use file = File.OpenRead(filename)
        printfn "Reading from file %s." filename
        do! file.AsyncRead(numBytes) |> Async.Ignore
    }

let filename = "BigFile.dat"
let numBytes = 100000000

let result1 = writeToFile filename numBytes
             |> Async.Catch
             |> Async.RunSynchronously
match result1 with
| Choice1Of2 _ -> printfn "Successfully wrote to file."; ()
| Choice2Of2 exn -> 
      printfn "Exception occurred writing to file %s: %s" filename exn.Message

// Start these next two operations asynchronously, forcing an exception due
// to trying to access the file twice simultaneously.
Async.Start(readFile filename numBytes)
let result2 = writeToFile filename numBytes
             |> Async.Catch
             |> Async.RunSynchronously
match result2 with
| Choice1Of2 buffer -> printfn "Successfully read from file."
| Choice2Of2 exn ->
    printfn "Exception occurred reading from file %s: %s" filename (exn.Message)

플랫폼

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

버전 정보

F# 런타임

지원되는 버전: 2.0, 4.0

Silverlight

지원되는 버전: 3

참고 항목

참조

Control.Async 클래스(F#)

Microsoft.FSharp.Control 네임스페이스(F#)

변경 기록

날짜

변경 내용

이유

2010년 8월

코드 예제를 추가했습니다.

향상된 기능 관련 정보