PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 5, 2022

[FIXED] What is inner Exception

 August 05, 2022     .net, c#, exception, inner-exception     No comments   

Issue

I have read the MSDN but, I could not understand this concept.

Correct me if I am wrong,

A innerexception will be used in hand with current exception.

Inner exception will occur first and then the current exception will occur (if there is an exception) that is the reason why InnerException is checked against null. In order to retain inner exception we have to pass it as a parameter.

Am I right with this?


Solution

You can see the code below.

First step, I parse "abc" to int. It will raise FormatException.

In the very first catch block (that handles the raised FormatException), I try to open a text file to log the exception message. But this file does not exist so a second exception is raised, this time of type FileNotFoundException.

I want to store what caused the second exception to be raised, so I add the first exception (fe of type FormatException) to the with the fe parameter (of type FormatException) passed in the FileNotFoundException(String, Exception) constructor of the second exception.

That constructor will store the first exception in the InnerException property of the second exception.

In the outermost catch block, I can access InnerException's properties to know what the first exception is.

Is it useful?

using System;
using System.IO;

public class Program
{
    public static void Main( )
    {
        try
        {
            try
            {
                var num = int.Parse("abc"); // Throws FormatException               
            }
            catch ( FormatException fe )
            {
                try
                {
                    var openLog = File.Open("DoesNotExist", FileMode.Open);
                }
                catch
                {
                    throw new FileNotFoundException("NestedExceptionMessage: File `DoesNotExist` not found.", fe );
                }                              
            }
        }
        // Consider what exception is thrown: FormatException or FileNotFoundException?
        catch ( FormatException fe )
        {
            // FormatException isn't caught because it's stored "inside" the FileNotFoundException
        }
        catch ( FileNotFoundException fnfe ) 
        {
            string innerMessage = "", outerMesage;
            if (fnfe.InnerException != null)
                innerMessage = fnfe.InnerException.Message; // Inner exception (FormatException) message
            outerMesage = fnfe.Message;
            Console.WriteLine($"Inner Exception:\n\t{innerMessage}");
            Console.WriteLine($"Outer Exception:\n\t{outerMesage}");
        }        
    }
}

Console Output

Inner Exception:
    Input string was not in a correct format.
Outer Exception:
    NestedExceptionMessage: File `DoesNotExist` not found.

The Inner exception refers to the deeper nested exception that is ultimately thrown. The Outer exception refers the most shallow (in scope) exception.



Answered By - Thinh Vu
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing