using System; public class MainClass { static void Main(string[] args) { unsafe { int variable = 10; int* pVariable = &variable; Console.WriteLine("Value at address is {0}.", *pVariable); } } }
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()); } } }
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 } } }