Sponsored Ad

Friday, December 17, 2010

How to Execute Next Statement After Error in C#

This Code will help to to bypass error and execute all other statements. For example in this scenario we have two statements. one is before error and another is after error. In normal case if any error occurs the control will not execute the next statements after errors. While if we use the try catch block and suppress the error then it will execute the next statements also. 

You can also handle the error in exception block as per your requirement.

How to Execute Next Statement After Error in C#

Code to Bypass Error and Execute Next Statement:

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Satement Before Error.");
            try
            {
                int i = Convert.ToInt16("Str");
            }
            catch (Exception ex)
            { }
            Console.WriteLine("Satement After Error.");
            Console.ReadLine();
        }    
    }
}

Friday, September 24, 2010

While Loop Program Using C# | While Loop Example

This simple program demonstrate the use of while loop in C#. While loop run until condition reaches to false.

image

Code for While Loop:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsWhileLoop
    {
        public static void Main()
        {
            try
            {
                Console.WriteLine("Enter a number:");
                int GivenNumber = Convert.ToInt16(Console.ReadLine());
                while (GivenNumber < 10)
                {
                    Console.WriteLine("Loop Count {0} ", GivenNumber);
                    GivenNumber++;
                }
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

How to use If Else in C# | If Else Example using C#

 

This is program of If Else complex and simple examples. This example shows the exact use of If Else statement in C#.

How to use If Else in C# | If Else Example using C#

Source Code of If Else Example:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsIfElase
    {
        public static void Main()
        {
            try
            {            
                int iInt;

                Console.Write("Enter a number for IF Else Example: ");            
                iInt = Int32.Parse(Console.ReadLine());
                if (iInt > 0)
                {
                    Console.WriteLine("{0} is greater than zero.", iInt);
                }

               if (iInt < 0)
                    Console.WriteLine("{0} is less than zero.", iInt);

                //simple example
                if (iInt != 0)
                {
                    Console.WriteLine("{0} is not equal to zero.", iInt);
                }
                else
                {
                    Console.WriteLine("{0} is equal to zero.", iInt);
                }
                //complex example
                if (iInt < 0 || iInt == 0)
                {
                    Console.WriteLine("{0} is less than or equal to zero.", iInt);
                }
                else if (iInt > 0 && iInt < 100)
                {
                    Console.WriteLine("{0} is between 1 and 100.", iInt);
                }
                else if (iInt > 101 && iInt < 200)
                {
                    Console.WriteLine("{0} is between 101 and 200.", iInt);
                }
                else if (iInt > 201 && iInt < 300)
                {
                    Console.WriteLine("{0} is between 201 and 300.", iInt);
                }
                else
                {
                    Console.WriteLine("{0} is greater than 300.", iInt);
                }
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

Sponsored Ad

Development Updates