Ad Code

Responsive Advertisement

Exception and Windows Programming

Exception & Windows Programming

Que. How can we raise the exception?
Ans. If we want to raise an exception follow the blow process –
  • Create an object of exception class.
  • Throw that object using Throw object.
  • Syntax – throw <object>
  • When we want to raise an exception explicitly, we can adopt to different approach
a.  Where we want to raise an exception than create an object of class ApplicationException by       passing an error massage as a parameter to its constructor.
ApplicationException (string error massage)

Eg:-
ApplicationException  obj = new ApplicationException (“< error massage>”)

throw obj;
or
throw new ApplicationException (“< error massage>”) 

.    b. First define a user define exception class as par your requirement and use that class in the place of application exception class same as the above.

Q. How to define an Exception class?
Ans. to define our own exception classes about the blow process-
1.      Define a class inheriting from the pre-defined class Exception so that the new class also             becomes an Exception class.
2.      Now override the virtual readonly property of Exception class under your new class as par           your requirement.
- Add a class ThrowDemo.cs and write the following:
    class OddNumberException : Exception
    {
        public override string  Message


     {
     get
     {
            return "odd number can't be used as Divisor";
     }
}
         
    }
   
    class ThrowDemo
    {
        static void Main()
        {
            try
            {
                int x, y, z;
                Console.Write("Enter x Value :");
                x = int.Parse(Console.ReadLine());
                Console.Write("Enter y Value :");
                y = int.Parse(Console.ReadLine());
                z = x / y;
                if (y % 2 > 0)
                {
                    //throw new ApplicationException("divisor can't be odd number");
                    throw new OddNumberException();
                }
                Console.WriteLine(z);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("End the Program");
            Console.ReadLine();
        }
    }
} 

Partial Class

It’s a new approach that came into picture from C# 2.0 which allows splitting a class into multiple files i.e. one class can be defined with in more than one file.
- To define ‘partial class’ we need to prefix the class with “partial” modifier.
- If we want to inherit a partial class from any other class it is enough to inheriting any single file but not all.
- Partial class is allowing multiple programmers to work on the same class at same time but on different files.
- Partial classes are used for physical separation of related code into different file, but logically they will be under the same class.
- Add a class Part1.cs and write the following:
using System;
namespace oopsProject
{
    public partial class Parts
    {
        public void Method1()
        {
            Console.WriteLine("Method 1");
        }
        public void Method2()
        {
            Console.WriteLine("Method 2");
        }
    }
} 
- Add a class Part2.cs and write the following:
using System;
namespace oopsProject
{
    public partial class Parts
    {
        public void Method3()
        {
            Console.WriteLine("Method 3");
        }
        public void Method4()
        {
            Console.WriteLine("Method 4");
        }
    }
} 
- Add a class TestParts.cs and write the following:
using System;
namespace oopsProject
{
    class TestParts
    {
        static void Main()
        {
            Parts p = new Parts();
            p.Method1(); p.Method2(); p.Method3(); p.Method4();
           
            Console.ReadLine();
        }
    }
} 

Reactions

Post a Comment

0 Comments