Professional Documents
Culture Documents
---------------------------------------------
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(Type x)
finally
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;
using System;
class MyClient
try
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
using System;
class MyClass
try
int x = 0;
catch (DivideByZeroException)
throw;
class MyClient
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.
using System;
}
class MyClient
try
catch (Exception)
Console.WriteLine("LAST STATEMENT");
----------------------------
finally – The finally block always executes when the try block exits, except System.exit(0) call. This
ensures that the finally block is executed
finalize() – method helps in garbage collection. A method that is invoked before an object is
discarded by the garbage collector, allowing it to
------------------------------
A dictionary is a collection of (key, value) pairs.
When creating a dictionary, we need to specify the type for the key and as well as for the value.
di.Add("Eno", 1001);
di.Add("Ename", "Pranaya");
di.Add("Job", "Developer");
di.Add("Salary", 7500);
di.Add("Location", "Mumbai");
Console.WriteLine();
//removes the specified key with the value from the dictionary
di.Remove("Job");
Console.ReadKey();