الاستثناءات: حاول... مع تعبير (F#)

This موضوع describes the try...with تعبير, the تعبير that هو used for ‏‏ استثناء handling في the F# اللغة.

try
 expression1
with
  | pattern1 -> expression2
  | pattern2 -> expression3
  ...

ملاحظات

The try...with تعبير هو used إلى مؤشر استثناءات في F#. في بناء الجملة السابق، والتعليمات البرمجية في expression1قد تنشئ إستثناء. The try...with تعبير إرجاع a القيمة. If لا ‏‏ استثناء هو تم طرح, the whole تعبير إرجاع the القيمة of expression1. If an ‏‏ استثناء هو تم طرح, each pattern هو compared في turn مع the ‏‏ استثناء, و for the أول matching نقش, the corresponding expression, known كـ the ‏‏ استثناء handler, for that فرع هو executed, و the overall تعبير إرجاع the القيمة of the تعبير في that ‏‏ استثناء handler. If لا نقش التلائمات, the ‏‏ استثناء propagates لأعلى the يتصل مكدس until a matching handler هو found. The أنواع of the قيم returned من each تعبير في the ‏‏ استثناء handlers must مطابقة the نوع returned من the تعبير في the try حظر.

Frequently, the fact that an خطأ occurred also means that there غير محقق صالح القيمة that can be returned من the expressions في each ‏‏ استثناء handler. A frequent نقش هو إلى have the نوع of the تعبير be an خيار نوع. The following تعليمات برمجية مثال illustrates this نقش.

let divide1 x y =
   try
      Some (x / y)
   with
      | :? System.DivideByZeroException -> printfn "Division by zero!"; None

let result1 = divide1 100 0

استثناءات can be .NET استثناءات, أو they can be F# استثناءات. You can define F# استثناءات بواسطة using the exception كلمة أساسية.

You can استخدم a variety of أنماط إلى عامل تصفية تشغيل the ‏‏ استثناء نوع و غير ذلك conditions; the خيارات are summarized في the following جدول.

النمط

الوصف

:? exception-type

التلائمات the specified .NET ‏‏ استثناء نوع.

:? exception-type as identifier

التلائمات the specified .NET ‏‏ استثناء نوع, but gives the ‏‏ استثناء a named القيمة.

exception-name(arguments)

التلائمات an F# ‏‏ استثناء نوع و binds the الوسيطات.

identifier

التلائمات أي ‏‏ استثناء و binds the اسم إلى the ‏‏ استثناء كائن. مُكافئ ل :? System.Exception as identifier

identifier عند condition

يطابق أي استثناء إذا كان الشرط هو صحيحاً.

أمثلة

توضح الأمثلة تعليمات برمجية التالية استخدم من النقوش معالج ‏‏ استثناء المتنوعة.

// This example shows the use of the as keyword to assign a name to a
// .NET exception.
let divide2 x y =
  try
    Some( x / y )
  with
    | :? System.DivideByZeroException as ex -> printfn "Exception! %s " (ex.Message); None

// This version shows the use of a condition to branch to multiple paths
// with the same exception.
let divide3 x y flag =
  try
     x / y
  with
     | ex when flag -> printfn "TRUE: %s" (ex.ToString()); 0
     | ex when not flag -> printfn "FALSE: %s" (ex.ToString()); 1

let result2 = divide3 100 0 true

// This version shows the use of F# exceptions.
exception Error1 of string
exception Error2 of string * int

let function1 x y =
   try
      if x = y then raise (Error1("x"))
      else raise (Error2("x", 10))
   with
      | Error1(str) -> printfn "Error1 %s" str
      | Error2(str, i) -> printfn "Error2 %s %d" str i

function1 10 10
function1 9 2

ملاحظة

try...withبناء هو تعبير منفصلة من try...finallyالتعبير. ولذلك، إذا الخاص بك تعليمات برمجية يتطلب كل من على withحظر finallyحظر، فيجب أن تداخل بين التعبيرات.

ملاحظة

يمكنك استخدام try...withفي مهام سير العمل غير المتزامنة وحساب تعبيرات غير ذلك، حيث الحالة الإصدار مخصص من try...withالتعبير هو المستخدمة. وللمزيد من المعلومات ، راجع مهام سير العمل غير متزامن (F#) و التعبيرات احتساب (F#) .

راجع أيضًا:

المرجع

أنواع الاستثناءات (F#)

الاستثناءات: حاول... وأخيراً تعبير (F#)

موارد أخرى

‏‏ استثناء معالجة (F#)