You are on page 1of 6

Virtual function and Function over loading

---------------------------------------------

The function which you want to do overload must be a virtual function. Otherwise, it will not
overload and overwrite.

Exception handling

------------------

C# Exception handling is a mechanism in .NET to detect and handle run time errors. The try..catch
block is used to implement exception handling in

C#. In try..catch..finally block, finally is used for code cleanup. Code sample for multiple try catch
block.

try..catch..finally

C# provides three keywords try, catch and finally to implement exception handling. The try encloses
the statements that might throw an exception

whereas catch handles an exception if one exists. The finally can be used for any cleanup work that
needs to be done.

Try..catch..finally block example:

try

// Statement which can cause an exception.

catch(Type x)

// Statements for handling the exception

finally

//Any cleanup code


}

Throwing an Exception

In C#, it is possible to throw an exception programmatically. The 'throw' keyword is used for this
purpose. The general form of throwing an exception

is as follows.

throw exception_obj;

For example, the following statement throws an ArgumentException explicitly.

throw new ArgumentException("Exception");

//C#: Exception Handling:

using System;

class MyClient

public static void Main()

try

throw new DivideByZeroException("Invalid Division");

catch (DivideByZeroException)

Console.WriteLine("Exception");

Console.WriteLine("LAST STATEMENT");

Re-throwing an Exception
The exceptions, which we caught inside a catch block, can re-throw to a higher context by using the
keyword throw inside the catch block. The

following program shows how to do this.

//C#: Exception Handling: Handling all exceptions

using System;

class MyClass

public void Method()

try

int x = 0;

int sum = 100 / x;

catch (DivideByZeroException)

throw;

class MyClient

public static void Main()

MyClass mc = new MyClass();

try

mc.Method();

catch (Exception)

{
Console.WriteLine("Exception caught here");

Console.WriteLine("LAST STATEMENT");

Standard Exceptions

System.OutOfMemoryException

System.NullReferenceException

Syste.InvalidCastException

Syste.ArrayTypeMismatchException

System.IndexOutOfRangeException

System.ArithmeticException

System.DevideByZeroException

System.OverFlowException

User-defined Exceptions

In C#, it is possible to create our own exception class. But Exception must be the ultimate base class
for all exceptions in C#. So the user-defined

exception classes must inherit from either Exception class or one of its standard derived classes.

//C#: Exception Handling: User defined exceptions

using System;

class MyException : Exception

public MyException(string str)

Console.WriteLine("User defined exception");

}
class MyClient

public static void Main()

try

throw new MyException("RAJESH");

catch (Exception)

Console.WriteLine("Exception caught here" + e.ToString());

Console.WriteLine("LAST STATEMENT");

Final, Finally and Finallize

----------------------------

final – constant declaration.

finally – The finally block always executes when the try block exits, except System.exit(0) call. This
ensures that the finally block is executed

even if an unexpected exception occurs.

finalize() – method helps in garbage collection. A method that is invoked before an object is
discarded by the garbage collector, allowing it to

clean up its state.

Dictionary Generic Collection:

------------------------------
A dictionary is a collection of (key, value) pairs.

The Dictionary Generic Collection class is present in System.Collections.Generic namespace.

When creating a dictionary, we need to specify the type for the key and as well as for the value.

The fastest way to find a value in a dictionary is by using the keys.

Keys in a dictionary must be unique.

Dictionary<string, object> di = new Dictionary<string, object>();

//adds the specified key and value to the dictionary

di.Add("Eno", 1001);

di.Add("Ename", "Pranaya");

di.Add("Job", "Developer");

di.Add("Salary", 7500);

di.Add("Location", "Mumbai");

//Loopig through each keys to get the values

foreach (string key in di.Keys)

Console.WriteLine(key + " : " + di[key]);

Console.WriteLine();

//removes the specified key with the value from the dictionary

Console.WriteLine("After Removing the Job Key : ");

di.Remove("Job");

foreach (string key in di.Keys)

Console.WriteLine(key + " : " + di[key]);

Console.ReadKey();

You might also like