Contributor.
Contributor.
528 views

C# Conversion

Jump to solution

Can someone convert this for me?  When I convert this and try to use it, my var exception is always null and it shouldn't be as I am purposefully causing an exception to test this out.  When it encounters the while statement (perform until in .net Cobol) it will bypass the sb.appendline as exception is always null.

 

public static class ExceptionExtension
        {
            public static string ToFullBlownString(this System.Exception e, int level = int.MaxValue)
            {
                var sb = new StringBuilder();
                var exception = e;
                var counter = 1;
                while (exception != null && counter <= level)
                { 
                  sb.AppendLine($"{counter}-> Level: {counter}");
                  sb.AppendLine($"{counter}-> Message: {exception.Message}");
                  sb.AppendLine($"{counter}-> Source: {exception.Source}");
                  sb.AppendLine($"{counter}-> Target Site: {exception.TargetSite}");
                  sb.AppendLine($"{counter}-> Stack Trace: {exception.StackTrace}");

                  exception = exception.InnerException;
                  counter++;
                }

            return sb.ToString();
            }
        }
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

The following works for me:

 

       program-id. Program1 as "testexcept.Program1".
       procedure division.

           try
              call "nothere"
           catch myex as type Exception
              display myex::ToFullBlownString
           end-try
           display "after exception"
           goback.
           
       end program Program1.

       class-id ExceptionExtension static.
       method-id ToFullBlownString extension (e as type System.Exception, level as binary-long = type Int32::MaxValue)
                                              returning result as string.
       procedure division.
           declare sb = new type StringBuilder
           declare #exception = e
           declare counter = 1
           perform until #exception = null or counter > level
             invoke sb::AppendLine(counter::ToString & "-> Level: " & counter::ToString)
             invoke sb::AppendLine(counter::ToString & "-> Message: " & #exception::Message)
             invoke sb::AppendLine(counter::ToString & "-> Source: " & #exception::Source)
             invoke sb::AppendLine(counter::ToString & "-> Target Site: " & #exception::TargetSite)
             invoke sb::AppendLine(counter::ToString & "-> Stack Trace: " & #exception::StackTrace)
             set #exception = #exception::InnerException
             set counter = counter + 1
           end-perform
           set result to sb::ToString
            
           goback.
       end method.
       
       end class.

View solution in original post

0 Likes
1 Reply
Highlighted
Micro Focus Expert
Micro Focus Expert

The following works for me:

 

       program-id. Program1 as "testexcept.Program1".
       procedure division.

           try
              call "nothere"
           catch myex as type Exception
              display myex::ToFullBlownString
           end-try
           display "after exception"
           goback.
           
       end program Program1.

       class-id ExceptionExtension static.
       method-id ToFullBlownString extension (e as type System.Exception, level as binary-long = type Int32::MaxValue)
                                              returning result as string.
       procedure division.
           declare sb = new type StringBuilder
           declare #exception = e
           declare counter = 1
           perform until #exception = null or counter > level
             invoke sb::AppendLine(counter::ToString & "-> Level: " & counter::ToString)
             invoke sb::AppendLine(counter::ToString & "-> Message: " & #exception::Message)
             invoke sb::AppendLine(counter::ToString & "-> Source: " & #exception::Source)
             invoke sb::AppendLine(counter::ToString & "-> Target Site: " & #exception::TargetSite)
             invoke sb::AppendLine(counter::ToString & "-> Stack Trace: " & #exception::StackTrace)
             set #exception = #exception::InnerException
             set counter = counter + 1
           end-perform
           set result to sb::ToString
            
           goback.
       end method.
       
       end class.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.