Solved

Is this exception handling a type of polymorphism?

Posted on 2013-10-31
5
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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