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()); } } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
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"); } } }
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"); } } }
Throw exception from getter
using System;
public class MyValue {
public String Name;
}
class CardDeck {
private MyValue[] Cards = new MyValue[52];
public MyValue GetCard(int idx) {
if ((idx >= 0) && (idx <= 51))
return Cards[idx];
else
throw new IndexOutOfRangeException("Invalid Card");
}
public static void Main(String[] args) {
try {
CardDeck PokerDeck = new CardDeck();
MyValue HiddenAce = PokerDeck.GetCard(53);
} catch (IndexOutOfRangeException e) {
Console.WriteLine(e.Message);
} catch (Exception e) {
Console.WriteLine(e.Message);
} finally {
// Cleanup code
}
}
}
[/csharp]
Throw and catch Exception
using System; class ExceptionThrower { static void MethodOne() { try { MethodTwo(); } finally { } } static void MethodTwo() { throw new Exception("Exception Thrown in Method Two"); } public static void Main(String[] args) { try { ExceptionThrower FooBar = new ExceptionThrower(); MethodOne(); } catch (Exception e) { Console.WriteLine(e.Message); } finally { // Cleanup code } } }
Exception Translation: CLR catches an exception and rethrows a different exception. The inner exception contains the original exception.
using System; using System.Reflection; public class MyClass { public static void MethodA() { Console.WriteLine("MyClass.MethodA"); throw new Exception("MethodA exception"); } } public class Starter { public static void Main() { try { Type zType = typeof(MyClass); MethodInfo method = zType.GetMethod("MethodA"); method.Invoke(null, null); } catch (Exception except) { Console.WriteLine(except.Message); Console.WriteLine("original: " + except.InnerException.Message); } } }