Solved

Is this exception handling a type of polymorphism?

Posted on 2013-10-31
5
343 Views
Last Modified: 2013-11-01
If this code throws a FileNotFoundException, would catching it as type Exception be considered Polymorphism? I understand Polymorphism as being able to invoke derived class methods from a base class variable, so this could look something like
Exception e = new FileNotFoundException(); ?

    
    class ExceptionHandlingExample
    {
        static void Main(string[] args)
        {
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(@"C:\Sample Files\Data.txt");
                Console.WriteLine(sr.ReadToEnd());                
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("Please check if the file {0} exists.", e.FileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally //Will run even if there is an additional exception in a catch block
            {
                if (sr != null) { sr.Close(); }
                Console.WriteLine("Finally Block");
                Console.ReadLine();
            }
        }
    }

Open in new window

0
Comment
Question by:nightshadz
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:Ironhoofs
ID: 39614006
I am not sure what you are asking, but i'll try

Specific exceptions (like FileNotFoundException) are derived from Exception. Its a easy way to get additional information about an error if you want, but you don't have to.

You can catch the default Exception and convert back to the specific exception, but the additional information has been lost. Example:
try {
	throw new System.IO.FileNotFoundException("test.xml");
}
catch (Exception e) {
	System.IO.FileNotFoundException ex = e as System.IO.FileNotFoundException;
	MessageBox.Show("FileNotFound " + ex.FileName);
}

Open in new window

0
 

Author Comment

by:nightshadz
ID: 39614018
Hi, thanks for your response. I'm just asking if catching a FileNotFoundException as type Exception would be considered a form of Polymorphism as shown above?
0
 
LVL 7

Assisted Solution

by:Ironhoofs
Ironhoofs earned 250 total points
ID: 39614052
I think the  "Catch" in your example  can be considered polymorphism (see http://en.wikipedia.org/wiki/Polymorphism_(computer_science)), but the two types of exceptions are the result  of inheritance.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39616069
Not, you are not using polymorphism in that code, at least not in for a FileNotFoundException.

Because of the way Try...Catch was designed by it's designers, only one Catch can fire, the first one that can grab the Exception that was thrown.

Since you trap the object from the derived class (FileNotFoundException) first, a FileNofFoundException will never get to the part of the code where you trap the base class (Exception).

However, you are using polymorphism for any error other than FileNotFoundException. Since all the Exception classes derive from Exception, Exception will catch any of them.
0
 

Author Comment

by:nightshadz
ID: 39616683
I meant to remove the FileNotFoundException clause. Thanks for your input guys!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question