Solved

Is this exception handling a type of polymorphism?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

19 Experts available now in Live!

Get 1:1 Help Now