Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
using System;
using System.IO;

namespace InnerExceptionHandling
{
    class Program
    {
        static void Main()
        {
            try
            {
                Console.Write("Enter the first number:\t\t");
                int FN = int.Parse(Console.ReadLine());
                Console.Write("Enter the second number:\t");
                int SN = int.Parse(Console.ReadLine());
                int result = FN / SN;
                Console.WriteLine("{0} / {1} = {2}", FN, SN, result);
            }
            catch (Exception ex)
            {
                string filePath= @"C:\Users\BISTALAPTOP1\Desktop\bista\InnerException.txt";

                StreamWriter SW = new StreamWriter(filePath);
                SW.Write(ex.GetType().Name);
                SW.WriteLine(ex.Message);
                SW.Close();
                
                
            }
        }
    }
}


What I have tried:

I have tried using C# to print the Exception on a file
Posted
Updated 22-Feb-21 4:26am
Comments
[no name] 22-Feb-21 9:55am    
And the problem manifests how?
20212a 22-Feb-21 10:14am    
What is the question?

Here for an example on how to use StreamWriter and write text to log file: StreamWriter Class (System.IO) | Microsoft Docs[^]
C#
namespace StreamReadWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the directories currently on the C drive.
            DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();

            // Write each directory name to a file.
            using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))
            {
                foreach (DirectoryInfo dir in cDirs)
                {
                    sw.WriteLine(dir.Name);
                }
            }

            // Read and show each line from the file.
            string line = "";
            using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
    }
}


Another sample here: Exception Logging to Text file[^]
 
Share this answer
 
Comments
[no name] 22-Feb-21 10:26am    
But what should be the problem in OP's code? Apart from, that maybe the target directory does not exists? :-)
Sandeep Mewara 22-Feb-21 23:17pm    
1. Mostly it's path
2. Smaple is a cleaner way to do it. Releasing the streamwriter object
Most likely, it's the path that's a problem.
If I try your code with a path that I know is valid on my machine it works.

Check your path, and make sure that the user account your code is executing under has the required permission to read and write that file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900