9 Replies - 560 Views - Last Post: 27 April 2019 - 04:11 PM Rate Topic: -----

#1 isla.star   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-September 16

How to throw and verify an NotImplementedException

Posted 25 April 2019 - 03:05 AM

 static void GetStudentBirthday()

        {
            Console.WriteLine("Enter the student's birthday: (mmddyyyy)");
            string birthDay = Console.ReadLine();

            if (string.IsNullOrEmpty(birthDay))
            {
              
                Console.WriteLine("Birthday can't be empty! Input your birthday once more");
                birthDay = Console.ReadLine();
                //throw new NotImplementedException(("Birthday can't be empty! Input your birthday once more"));

            }
            else
            Console.WriteLine($"Birthday:, {birthDay}");
           


Is This A Good Question/Topic? 0
  • +

Replies To: How to throw and verify an NotImplementedException

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,093
  • Joined: 12-December 12

Re: How to throw and verify an NotImplementedException

Posted 25 April 2019 - 03:26 AM

You have commented code that throws the exception, uncomment it and see if it is thrown.

If you want to do something with the exception then you need to catch if. Basic exception's tutorials will have examples of catching.
[What book or tutorial are you studying?]
Was This Post Helpful? 0
  • +
  • -

#3 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: How to throw and verify an NotImplementedException

Posted 25 April 2019 - 04:26 AM

Before calling bad practice, I am going to ask why you are throwing exceptions, and more importantly exceptions of this type? What are you trying to achieve by this? What is wrong with using MessageBox or a custom form notification?

When you run this code (uncommented), it's not going to give you any any notification, and your form (assuming WinForms), will simply close. However, wrapping your commented line in a Try Catch Finally will give your user some instructions. However, this is not how Try Catch's are meant to be used... and my question still stands as to why you want to throw this exception type?
            try
            {
                throw new NotImplementedException(("Birthday can't be empty! Input your birthday once more"));
            }
            catch (NotImplementedException x)
            {
                MessageBox.Show(x.Message);
            }
            finally
            {

            }

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2869
  • View blog
  • Posts: 11,233
  • Joined: 03-December 12

Re: How to throw and verify an NotImplementedException

Posted 25 April 2019 - 05:00 AM

A not implemented exception means, the method doesn't have a body or doesn't exist, not the user didn't provide it. So, what is the intent of throwing the exception to begin with, especially if you are doing so after you ask again?
Was This Post Helpful? 1
  • +
  • -

#5 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: How to throw and verify an NotImplementedException

Posted 25 April 2019 - 06:59 AM

My reply earlier was made from my phone, which has a broken screen. Makes reading difficult. To clarify; regarding popping a messagebox in a console app. (Not sure why you'd want to) but here's one way to do it if you want to avoid the hassle of implanting references to Winforms, and adding the using directives thereafter.
using System;
using System.Runtime.InteropServices;

namespace TestConsoleApp
{
    class Program
    {
        [DllImport("User32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(IntPtr intPtr, string uMsg, string sTle, int iType);
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the student's birthday: (mmddyyyy)");
            string birthDay = Console.ReadLine();
            if (string.IsNullOrEmpty(birthDay))
            {
                Console.WriteLine("Birthday can't be empty! Input your birthday once more");
                birthDay = Console.ReadLine();
                try
                {
                    throw new NotImplementedException("Birthday can't be empty! Input your birthday once more");
                }
                catch (NotImplementedException x)
                {
                    MessageBox((IntPtr)0, x.Message, "My App Title", 0);
                }
                finally
                {
                    //Some work to finish
                }
            }
            else
                Console.WriteLine($"Birthday: {birthDay}");
        }
    }
}

It still makes no sense to do throw exceptions but there you are. A cleaner version below without exceptions ::
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the student's birthday: (mmddyyyy)");
            string birthDay = Console.ReadLine();
            if (string.IsNullOrEmpty(birthDay))
            {
                string s = "Birthday can't be empty! Input your birthday once more";
                Console.WriteLine(s);
                MessageBox((IntPtr)0, s, "My App Title", 0);
                birthDay = Console.ReadLine();
                Console.WriteLine($"Birthday: {birthDay}");
            }
            else
                Console.WriteLine($"Birthday: {birthDay}");
        }

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6963
  • View blog
  • Posts: 23,674
  • Joined: 05-May 12

Re: How to throw and verify an NotImplementedException

Posted 25 April 2019 - 09:24 AM

In general, exceptions should only be used for "exceptional" conditions. It shouldn't really be used for flow control. For things that your code can detect and handle, just do exactly that: detect the condition and handle it -- don't throw an exception.

Unfortunately, most books/tutorial which teach about exceptions explicitly setup something which can be detected and handled, but in their case the handling is throwing the exception. I think the authors feel that this makes for a more interactive tutorial or code base that hopefully their readers will continue to play and experiment with instead of just taking at face value.

Anyway for the code above, the more appropriate thing to do is to use DateTime.TryParseExact() (or DateType.TryParse()) in something like this:
do
{
    Console.Write("Enter birth date in MMDDYYYY format: ");
    input = Console.ReadLine();
    input = input?.Trim();
} while (DateTime.TryParseExact(input,
                                "MMddyyyy",
                                CultureInfo.InvariantCulture,
                                DateTimeStyles.None, out DateTime birthDate));




But if you really must use exceptions, then you would have something like:
bool parsed = false;
DateTime birthDate;
do
{
    Console.Write("Enter birth date in MMDDYYYY format: ");
    input = Console.ReadLine();
    input = input?.Trim();
    try
    {
        birthDate = DateTime.ParseExact(input, "MMddyyyy", CultureInfo.InvariantCulture);
        parsed = true;
    }
    catch (FormatException ex)
    {
        Console.Error.WriteLine($"Format exception thrown: {ex.Message}");
        parsed = false;
    }
} while (!parsed);


Was This Post Helpful? 2
  • +
  • -

#7 isla.star   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-September 16

Re: How to throw and verify an NotImplementedException

Posted 25 April 2019 - 10:10 AM

Thank you all for your response.
i want to understand exception handeling
Was This Post Helpful? 0
  • +
  • -

#8 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: How to throw and verify an NotImplementedException

Posted 25 April 2019 - 12:01 PM

+1 Skydiver - explained better than i did.

isla.star - A good start would be this :: Handling and throwing exceptions in .NET and afterwards, this :: Exception Class. And if you are really still interested, there are a bunch of books on the subject if you google about. Most books have no dedication to only that subject because exceptions have no standard way of working, as each language and IDE works different. But there is CLR via C# Fourth Edition on Chapter 20

Hope this helps. Or do you have a more direct and specific question re exceptions?

This post has been edited by Sheepings: 25 April 2019 - 12:03 PM

Was This Post Helpful? 1
  • +
  • -

#9 isla.star   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-September 16

Re: How to throw and verify an NotImplementedException

Posted 27 April 2019 - 09:54 AM

thanks

I shall take a look at it :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#10 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: How to throw and verify an NotImplementedException

Posted 27 April 2019 - 04:11 PM

You're welcome. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1