Can I Add Error Handling To An Existing Partial Class?












1















I have an existing partial class that has no error handling (any errors are simply thrown to the consuming code).



In the example below, I would like to add error management to MyClass to provide better information to the consuming class. (I have already added a Helper partial class to MyClass for other reasons).



What I can't work how to do - if it's possible - is how to extend the partial class with error management (trapping, conversion to a class error property, etc).



Example:



partial class MyClass
{
static void MyMethod()
{
throw new Exception("An error from MyClass.MyMethod");
}

void MyTest()
{
MyMethod();
}
}


Here, when MyTest.MyMethod() is called, an unhandled exception is thrown.



Ideally, I would be able to add a partial extension to MyClass which traps and manages such errors within the class. Obviously, I could wrap the class and manage the errors there, and perhaps the best/only way to do it, but I'd like opinions.



NOTE: Let's assume I have no control over MyClass tho in reality, in this instance, it's code generated by Swagger CodeGen, so I kinda do, but I'd rather not break into code generation any more than I already have.










share|improve this question























  • I'm not really sure I understand the premise of the question. If you're adding a partial class, can't you just add whatever you want to it? What have you tried? What's not working vs your expectations?

    – gilliduck
    Jan 20 at 3:00











  • @gilliduck Yes, sure I can add to the class, but I can't, say, intercept the throwing of an exception from a method within the original (partial) class (that I know of). And, yes, of course I've had a go and searched the web and SO; I'm here asking because I haven't arrived at a solution.

    – SteveCinq
    Jan 20 at 3:35
















1















I have an existing partial class that has no error handling (any errors are simply thrown to the consuming code).



In the example below, I would like to add error management to MyClass to provide better information to the consuming class. (I have already added a Helper partial class to MyClass for other reasons).



What I can't work how to do - if it's possible - is how to extend the partial class with error management (trapping, conversion to a class error property, etc).



Example:



partial class MyClass
{
static void MyMethod()
{
throw new Exception("An error from MyClass.MyMethod");
}

void MyTest()
{
MyMethod();
}
}


Here, when MyTest.MyMethod() is called, an unhandled exception is thrown.



Ideally, I would be able to add a partial extension to MyClass which traps and manages such errors within the class. Obviously, I could wrap the class and manage the errors there, and perhaps the best/only way to do it, but I'd like opinions.



NOTE: Let's assume I have no control over MyClass tho in reality, in this instance, it's code generated by Swagger CodeGen, so I kinda do, but I'd rather not break into code generation any more than I already have.










share|improve this question























  • I'm not really sure I understand the premise of the question. If you're adding a partial class, can't you just add whatever you want to it? What have you tried? What's not working vs your expectations?

    – gilliduck
    Jan 20 at 3:00











  • @gilliduck Yes, sure I can add to the class, but I can't, say, intercept the throwing of an exception from a method within the original (partial) class (that I know of). And, yes, of course I've had a go and searched the web and SO; I'm here asking because I haven't arrived at a solution.

    – SteveCinq
    Jan 20 at 3:35














1












1








1








I have an existing partial class that has no error handling (any errors are simply thrown to the consuming code).



In the example below, I would like to add error management to MyClass to provide better information to the consuming class. (I have already added a Helper partial class to MyClass for other reasons).



What I can't work how to do - if it's possible - is how to extend the partial class with error management (trapping, conversion to a class error property, etc).



Example:



partial class MyClass
{
static void MyMethod()
{
throw new Exception("An error from MyClass.MyMethod");
}

void MyTest()
{
MyMethod();
}
}


Here, when MyTest.MyMethod() is called, an unhandled exception is thrown.



Ideally, I would be able to add a partial extension to MyClass which traps and manages such errors within the class. Obviously, I could wrap the class and manage the errors there, and perhaps the best/only way to do it, but I'd like opinions.



NOTE: Let's assume I have no control over MyClass tho in reality, in this instance, it's code generated by Swagger CodeGen, so I kinda do, but I'd rather not break into code generation any more than I already have.










share|improve this question














I have an existing partial class that has no error handling (any errors are simply thrown to the consuming code).



In the example below, I would like to add error management to MyClass to provide better information to the consuming class. (I have already added a Helper partial class to MyClass for other reasons).



What I can't work how to do - if it's possible - is how to extend the partial class with error management (trapping, conversion to a class error property, etc).



Example:



partial class MyClass
{
static void MyMethod()
{
throw new Exception("An error from MyClass.MyMethod");
}

void MyTest()
{
MyMethod();
}
}


Here, when MyTest.MyMethod() is called, an unhandled exception is thrown.



Ideally, I would be able to add a partial extension to MyClass which traps and manages such errors within the class. Obviously, I could wrap the class and manage the errors there, and perhaps the best/only way to do it, but I'd like opinions.



NOTE: Let's assume I have no control over MyClass tho in reality, in this instance, it's code generated by Swagger CodeGen, so I kinda do, but I'd rather not break into code generation any more than I already have.







c# error-handling






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 0:23









SteveCinqSteveCinq

556612




556612













  • I'm not really sure I understand the premise of the question. If you're adding a partial class, can't you just add whatever you want to it? What have you tried? What's not working vs your expectations?

    – gilliduck
    Jan 20 at 3:00











  • @gilliduck Yes, sure I can add to the class, but I can't, say, intercept the throwing of an exception from a method within the original (partial) class (that I know of). And, yes, of course I've had a go and searched the web and SO; I'm here asking because I haven't arrived at a solution.

    – SteveCinq
    Jan 20 at 3:35



















  • I'm not really sure I understand the premise of the question. If you're adding a partial class, can't you just add whatever you want to it? What have you tried? What's not working vs your expectations?

    – gilliduck
    Jan 20 at 3:00











  • @gilliduck Yes, sure I can add to the class, but I can't, say, intercept the throwing of an exception from a method within the original (partial) class (that I know of). And, yes, of course I've had a go and searched the web and SO; I'm here asking because I haven't arrived at a solution.

    – SteveCinq
    Jan 20 at 3:35

















I'm not really sure I understand the premise of the question. If you're adding a partial class, can't you just add whatever you want to it? What have you tried? What's not working vs your expectations?

– gilliduck
Jan 20 at 3:00





I'm not really sure I understand the premise of the question. If you're adding a partial class, can't you just add whatever you want to it? What have you tried? What's not working vs your expectations?

– gilliduck
Jan 20 at 3:00













@gilliduck Yes, sure I can add to the class, but I can't, say, intercept the throwing of an exception from a method within the original (partial) class (that I know of). And, yes, of course I've had a go and searched the web and SO; I'm here asking because I haven't arrived at a solution.

– SteveCinq
Jan 20 at 3:35





@gilliduck Yes, sure I can add to the class, but I can't, say, intercept the throwing of an exception from a method within the original (partial) class (that I know of). And, yes, of course I've had a go and searched the web and SO; I'm here asking because I haven't arrived at a solution.

– SteveCinq
Jan 20 at 3:35












2 Answers
2






active

oldest

votes


















1














If I understand, you are asking if you can manipulate method invocations/control flow by adding something in a second partial class fie without any modification to the first file. As far as I am aware -- You cannot manipulate existing methods or the control flow from a partial class. Partial classes only allow you to split the definition into multiple files. I believe the only way to add error handling, without modifying the original methods, is to adapt or wrap (or inherit if they happen to be virtual).






share|improve this answer
























  • Thanks. Your reading of the question is correct. That's what I expected, but I wanted input from better C# coders than I. I've since read about some third-party class wrappers which can provide class error handling but I don't want to add in a TP reference.

    – SteveCinq
    Jan 20 at 3:37





















1














I think the best solutions will be inheritance from andor writing wrapper of your class.



Other methods may be little crazy, like



https://doc.postsharp.net/method-decorator



https://www.codeproject.com/Articles/16359/%2FArticles%2F16359%2FMethodLogger-Hook-into-method-calls-in-NET-binarie



However also it may be better just to handle exceptions in consuming code.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272539%2fcan-i-add-error-handling-to-an-existing-partial-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If I understand, you are asking if you can manipulate method invocations/control flow by adding something in a second partial class fie without any modification to the first file. As far as I am aware -- You cannot manipulate existing methods or the control flow from a partial class. Partial classes only allow you to split the definition into multiple files. I believe the only way to add error handling, without modifying the original methods, is to adapt or wrap (or inherit if they happen to be virtual).






    share|improve this answer
























    • Thanks. Your reading of the question is correct. That's what I expected, but I wanted input from better C# coders than I. I've since read about some third-party class wrappers which can provide class error handling but I don't want to add in a TP reference.

      – SteveCinq
      Jan 20 at 3:37


















    1














    If I understand, you are asking if you can manipulate method invocations/control flow by adding something in a second partial class fie without any modification to the first file. As far as I am aware -- You cannot manipulate existing methods or the control flow from a partial class. Partial classes only allow you to split the definition into multiple files. I believe the only way to add error handling, without modifying the original methods, is to adapt or wrap (or inherit if they happen to be virtual).






    share|improve this answer
























    • Thanks. Your reading of the question is correct. That's what I expected, but I wanted input from better C# coders than I. I've since read about some third-party class wrappers which can provide class error handling but I don't want to add in a TP reference.

      – SteveCinq
      Jan 20 at 3:37
















    1












    1








    1







    If I understand, you are asking if you can manipulate method invocations/control flow by adding something in a second partial class fie without any modification to the first file. As far as I am aware -- You cannot manipulate existing methods or the control flow from a partial class. Partial classes only allow you to split the definition into multiple files. I believe the only way to add error handling, without modifying the original methods, is to adapt or wrap (or inherit if they happen to be virtual).






    share|improve this answer













    If I understand, you are asking if you can manipulate method invocations/control flow by adding something in a second partial class fie without any modification to the first file. As far as I am aware -- You cannot manipulate existing methods or the control flow from a partial class. Partial classes only allow you to split the definition into multiple files. I believe the only way to add error handling, without modifying the original methods, is to adapt or wrap (or inherit if they happen to be virtual).







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 20 at 3:20









    Jonathon KJonathon K

    715




    715













    • Thanks. Your reading of the question is correct. That's what I expected, but I wanted input from better C# coders than I. I've since read about some third-party class wrappers which can provide class error handling but I don't want to add in a TP reference.

      – SteveCinq
      Jan 20 at 3:37





















    • Thanks. Your reading of the question is correct. That's what I expected, but I wanted input from better C# coders than I. I've since read about some third-party class wrappers which can provide class error handling but I don't want to add in a TP reference.

      – SteveCinq
      Jan 20 at 3:37



















    Thanks. Your reading of the question is correct. That's what I expected, but I wanted input from better C# coders than I. I've since read about some third-party class wrappers which can provide class error handling but I don't want to add in a TP reference.

    – SteveCinq
    Jan 20 at 3:37







    Thanks. Your reading of the question is correct. That's what I expected, but I wanted input from better C# coders than I. I've since read about some third-party class wrappers which can provide class error handling but I don't want to add in a TP reference.

    – SteveCinq
    Jan 20 at 3:37















    1














    I think the best solutions will be inheritance from andor writing wrapper of your class.



    Other methods may be little crazy, like



    https://doc.postsharp.net/method-decorator



    https://www.codeproject.com/Articles/16359/%2FArticles%2F16359%2FMethodLogger-Hook-into-method-calls-in-NET-binarie



    However also it may be better just to handle exceptions in consuming code.






    share|improve this answer




























      1














      I think the best solutions will be inheritance from andor writing wrapper of your class.



      Other methods may be little crazy, like



      https://doc.postsharp.net/method-decorator



      https://www.codeproject.com/Articles/16359/%2FArticles%2F16359%2FMethodLogger-Hook-into-method-calls-in-NET-binarie



      However also it may be better just to handle exceptions in consuming code.






      share|improve this answer


























        1












        1








        1







        I think the best solutions will be inheritance from andor writing wrapper of your class.



        Other methods may be little crazy, like



        https://doc.postsharp.net/method-decorator



        https://www.codeproject.com/Articles/16359/%2FArticles%2F16359%2FMethodLogger-Hook-into-method-calls-in-NET-binarie



        However also it may be better just to handle exceptions in consuming code.






        share|improve this answer













        I think the best solutions will be inheritance from andor writing wrapper of your class.



        Other methods may be little crazy, like



        https://doc.postsharp.net/method-decorator



        https://www.codeproject.com/Articles/16359/%2FArticles%2F16359%2FMethodLogger-Hook-into-method-calls-in-NET-binarie



        However also it may be better just to handle exceptions in consuming code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 20 at 14:45









        woldemarwoldemar

        6747




        6747






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272539%2fcan-i-add-error-handling-to-an-existing-partial-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre