C# notes part ->5


Exception Handling

An exception is a erroneous situation that occurs during program execution. When an exception occurs in an application, the system throws an error. The error is handled through the process of exception handling.
Error:which stops normal exception of a program.
Types of Errors:
1.  Compile time/syntax error
2.  Runtime error
3.  Logical error
1.          Syntax errors are the errors which occur when statements are not constructed properly.
2.          Runtime errors are the errors which occur when an application attempts to perform an operation which is not allowed at runtime.
3.          Logical errors are the errors which occurs when an application  compiles and runs properly but doesn’t produce the expected results. These are also called as bugs. In order to trace out the bugs we have a procedure called debugging process.
In debug menu of visual studio we have debugging o0ptions  like step into (F11) and step over (F10).
Step into à line by line execution
Step overà to avoid particular function

Runtime errors:
In order to hold the runtime errors we have a mechanism called exception handling. Exception  handling can be handled by using try-catch-finally blocks.

try
{
       //set of statements where occurs error
}
catch
{
       //caught the exception
}
finally
{
       //always executable code
}
Note: Every Exception class which will inherited from Exception base class.

Exception
|
OverFlow Exception
FileNotFound Exception
OutOfRange Exception
InvalidCast Exception
DivideByFlow Exception


using System;
namespace exceptionex
{
    class Program
    {
        static void Main(string[] args)
        {
            byte b;
            Console.WriteLine("Enter  b value");
            try
            {
                b = Convert.ToByte(Console.ReadLine());
                Console.WriteLine("the value of b is {0}", b);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                Console.WriteLine("Always Enter b value [1-255] only");
            }
            finally
            {
                Console.WriteLine("these statements always executed");
            }
            Console.ReadLine();
        }
    }
}


Note :  every exception class which are derived from
System.Exception class directly or indirectly.

System.Exception
|
|
1.      system.ApplicationException  àcustomised
2.      System.SystemException   à default

Custom defined exception is a mechanism for defining the user friendly error messages instead of depending on System Exception.

using System.Text;
namespace CustomException
{
    public class BankException:ApplicationException
    {
        int accno, balance, minbalance;
        public BankException(int accno, int balance, int minbalance)
        {
            this.accno = accno;
            this.balance = balance;
            this.minbalance = minbalance;

        }
        public void Inform()
        {
            Console.WriteLine("Account No  {0}, Balance  {1}", accno, balance);
            Console.WriteLine("please maintain minimum balance  {0}", minbalance);

        }

    }
    public class Customer
    {
        int accno, balance;
        string name;
        public Customer(int accno, string name, int balance)
        {
            this.accno = accno;
            this.name = name;
            this.balance = balance;
        }
        public void WithDraw(int amt)
        {
            int minbalance = 500;
            if (balance-amt<=minbalance)
            {
                throw new BankException(accno,balance,minbalance);
            }
            else
            {
                balance=balance-amt;
                Console.WriteLine("your balance after withdrawal of {0} is {1}", amt, balance);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer c = new Customer(100, "Rajitha", 5000);
            try
            {
                c.WithDraw(5000);
                Console.WriteLine("your transaction completed");
            }
            catch (BankException ex)
            {
                ex.Inform();
                Console.WriteLine("your transaction is incompleted");
            }
            Console.ReadLine();
        }

    }
}

threads
A thread is defined as execution path of a program.
Process is defined as a  running instance of a program.
Threads are used to run applications that perform large and complex competitions.
Single threaded process : a process which is executed using one thread is known as single threaded process. Only one task performed at a time. You have to wait for one task to complete before another task.(Synchronous)
Multi threaded process : A process that creates two or more threads is called as a multi threaded process.(Asynchronous)

System.Threading.Thread is required to interact with the threads.
This thread class contains properties and methods.
Start()
Sleep()
Wait()
Abort()
Suspend()
Resume()
Here all methods will be called Asynchronously.

using System;
using System.Threading;

namespace ThreadApp
{
    public class Sample
    {
        public void Display()
        {
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine("Item = "+i);
                Thread.Sleep(3000);
            }
        }
        public void Print()
        {
            for (int i = 10; i >= 0; i--)
            {
                Console.WriteLine("Item = " + i);
                Thread.Sleep(3000);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sample s = new Sample();
            Console.WriteLine("this is the Sample application for thread application");
            Thread t1 = new Thread(new ThreadStart(s.Display));
            t1.Start();
            t1.Suspend();
            t1.Resume();
            t1 = new Thread(new ThreadStart(s.Print));
            t1.Start();
            t1.Suspend();
            t1.Resume();
            Console.WriteLine("Hai");
            Console.ReadLine();
        }
    }
}


Life Cycle of a Thread

1.  Unstartable state
2.  Runnable state
3.  Unrunnable state
4.  Dead state



Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender