Csharp/C Sharp/Language Basics/Try Catch

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Catch Block With Exception Object

using System;
using System.Collections;
class Class1 {
   static void Main(string[] args) {
    long lngResult;
    long lngValue = 0;
    try {
      lngResult = 8 / lngValue;
    }
    catch (Exception objE) {
      Console.WriteLine(objE.ToString());
    }
   }
}


Catch without Exception type

    using System;
    using System.Collections;
class Class1 {
   static void Main(string[] args) {
    long lngResult;
    long lngValue = 0;
    try {
      lngResult = 8 / lngValue;
    }
    catch {
      Console.WriteLine("catch");
    }
    finally {
      Console.WriteLine("finally");
    }
   }
}


try and catch exception

    using System;
    using System.Collections;
class Class1 {
   static void Main(string[] args) {
    long lngResult;
    long lngValue = 0;
    try {
      lngResult = 8 / lngValue;
    }
    catch {
      Console.WriteLine("catch");
    }
    finally {
      Console.WriteLine("finally");
    }
   }
}


try catch with finally

    using System;
    using System.Collections;
class Class1 {
   static void Main(string[] args) {
    long lngResult;
    long lngValue = 0;
    try {
      lngResult = 8 / lngValue;
    }
    catch {
      Console.WriteLine("catch");
    }
    finally {
      Console.WriteLine("finally");
    }
   }
}