'>

Un Exemple d'exception (exercice resolu)

Le class de gestion de l'exception :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class ErreurAgeException : Exception
    {
        public ErreurAgeException(string message)  : base(message) {
        }
    }
}


Un class employe qui va exploite le class d'exception:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Employee
    {
        private  int id;
        private  string nom;
        private string prenom;
        private int age;

        public int Age
        {
            get { return age; }
            set {
                 if( value >= 18 &&  value <= 50 )
                {
                    age = value;
                }
                 else
                     try
                     {
                         throw new ErreurAgeException(" Age doit etre entre 18 et 50 ");
                        
                     }
                     catch (ErreurAgeException ex)
                     {
                         Console.WriteLine(ex.Message);
                     }
                    

               
            }
        }

        public Employee()
        { }

        public int Id {
            get { return id; }
            set {
                if (value >= 0)
                           
                   id = value;
           
               
                
           
            }
        }


        public string Nom
        {
            get { return nom; }
            set { nom = value; }
        }

        public string Prenom
        {
            get { return prenom; }
            set { prenom = value; }
        }

        public Employee( int id,string n, string p)
            {
                if (id >= 0)
                {
                    this.id = id;
                    nom = n;
                    prenom = p;
                }
               
                   
               
            }

        public override string ToString()
        {
            return id + " " + nom + " " + prenom;
        }

    }
}

Un programme de test:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Employee Ee = new Employee();
            Ee.Id = 15;
            Ee.Nom = "n1";
            Ee.Prenom = "p1";
            Ee.Age = 20;
            Console.WriteLine("ok c la fin");


            Ee.Id = -10;
            Employee E = new Employee(-10, "n", "p");
            Console.WriteLine(E.ToString());


            int a, b;


            try
            {
                Console.WriteLine("Enter a : ");
                a = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter b : ");
                b = int.Parse(Console.ReadLine());
                Console.WriteLine(a / b);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            finally
            {
                Console.WriteLine("Fin du programme");
            }
           

            Console.WriteLine("Ok");
          
          

           

            Console.ReadKey();
        }
    }
}



Publié par Drupal french Study