Csharp/C Sharp/Language Basics/Try Catch
Содержание
Catch Block With Exception Object
<source lang="csharp"> 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()); } }
}
</source>
Catch without Exception type
<source lang="csharp">
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"); } }
}
</source>
try and catch exception
<source lang="csharp">
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"); } }
}
</source>
try catch with finally
<source lang="csharp">
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"); } }
}
</source>