Solved

Is this exception handling a type of polymorphism?

Posted on 2013-10-31
5
334 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 49
Do you know how I could save the image array ELEMENTS as individual *.tiff files ? 2 37
C# Offline Apllication 5 48
Run time Error 4 34
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now