Exception throw

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nenad Dobrilovic

    Exception throw

    Hi,
    Is it possible for exception object to be aware of it's throwing?
    I want to log in the text file when exeption is thrown, not when the
    exception object is created (because I can create exception object that
    is never thrown).
    Thank you,
    Nenad
  • Marcin GrzÄ™bski

    #2
    Re: Exception throw

    Hi
    [color=blue]
    > Hi,
    > Is it possible for exception object to be aware of it's throwing?
    > I want to log in the text file when exeption is thrown, not when the
    > exception object is created (because I can create exception object that
    > is never thrown).
    > Thank you,
    > Nenad[/color]

    I think that there is no reason to create Exception object if it
    will not be thrown. But i don't want to settle "my own" coding
    standards.

    If you want to log exception's throw, so there is no better
    place to log it than the last (or first) catch of this exception.

    use:

    try {
    // YourException danger
    }
    catch(YourExcep tion ex) {
    // place for logging to text file
    throw;
    }

    Regards

    Marcin

    Comment

    • David Levine

      #3
      Re: Exception throw

      There currently is no means for the exception object itself to detect when
      it is thrown, but it is easy for your code to explicitly log it when it
      throws the exception. What are you trying to accomplish?


      "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
      news:uhMjaqEVEH A.3024@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi,
      > Is it possible for exception object to be aware of it's throwing?
      > I want to log in the text file when exeption is thrown, not when the
      > exception object is created (because I can create exception object that
      > is never thrown).
      > Thank you,
      > Nenad[/color]


      Comment

      • Leon Lambert

        #4
        Re: Exception throw

        Most people create their own application exceptions instead of using the
        generic System.Exceptio n so i will assume you did also. What you could
        do is create a Factory method on you exception class and have it create
        the exception, log to somewhere then throw the exception. Following is a
        short example.
        class MyException : Exception{
        public MyException(Str ing msg) : base(msg){
        }
        public static void ExceptionFactor y(String msg){
        MyException except = new MyException(msg );
        // log it
        System.Console. Out.WriteLine(m sg);
        throw(except);
        }
        }

        call it like this.
        MyException.Exc eptionFactory(" Test Exception");


        Just an idea hope it helps.
        Leon Lambert

        Nenad Dobrilovic wrote:[color=blue]
        > Hi,
        > Is it possible for exception object to be aware of it's throwing?
        > I want to log in the text file when exeption is thrown, not when the
        > exception object is created (because I can create exception object that
        > is never thrown).
        > Thank you,
        > Nenad[/color]

        Comment

        • Nenad Dobrilovic

          #5
          Re: Exception throw

          Leon Lambert wrote:
          [color=blue]
          > Most people create their own application exceptions instead of using the
          > generic System.Exceptio n so i will assume you did also. What you could
          > do is create a Factory method on you exception class and have it create
          > the exception, log to somewhere then throw the exception. Following is a
          > short example.
          > class MyException : Exception{
          > public MyException(Str ing msg) : base(msg){
          > }
          > public static void ExceptionFactor y(String msg){
          > MyException except = new MyException(msg );
          > // log it
          > System.Console. Out.WriteLine(m sg);
          > throw(except);
          > }
          > }
          >
          > call it like this.
          > MyException.Exc eptionFactory(" Test Exception");
          >
          >
          > Just an idea hope it helps.
          > Leon Lambert
          >
          > Nenad Dobrilovic wrote:
          >[color=green]
          >> Hi,
          >> Is it possible for exception object to be aware of it's throwing?
          >> I want to log in the text file when exeption is thrown, not when the
          >> exception object is created (because I can create exception object
          >> that is never thrown).
          >> Thank you,
          >> Nenad[/color][/color]

          Thank you,

          OK, I did it the same way you told me, but just before I read it :)
          I think it is goog idea.

          Nenad

          Comment

          • Nenad Dobrilovic

            #6
            Re: Exception throw

            David Levine wrote:
            [color=blue]
            > There currently is no means for the exception object itself to detect when
            > it is thrown, but it is easy for your code to explicitly log it when it
            > throws the exception. What are you trying to accomplish?
            >
            >
            > "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
            > news:uhMjaqEVEH A.3024@TK2MSFTN GP09.phx.gbl...
            >[color=green]
            >>Hi,
            >>Is it possible for exception object to be aware of it's throwing?
            >>I want to log in the text file when exeption is thrown, not when the
            >>exception object is created (because I can create exception object that
            >>is never thrown).
            >>Thank you,
            >>Nenad[/color]
            >
            >
            >[/color]

            I want that when someone write:
            throw new LoggedException (...);
            it is automatically logged in text file.

            Comment

            • Nenad Dobrilovic

              #7
              Re: Exception throw

              Marcin Grzębski wrote:
              [color=blue]
              > Hi
              >[color=green]
              >> Hi,
              >> Is it possible for exception object to be aware of it's throwing?
              >> I want to log in the text file when exeption is thrown, not when the
              >> exception object is created (because I can create exception object
              >> that is never thrown).
              >> Thank you,
              >> Nenad[/color]
              >
              >
              > I think that there is no reason to create Exception object if it
              > will not be thrown. But i don't want to settle "my own" coding
              > standards.
              >
              > If you want to log exception's throw, so there is no better
              > place to log it than the last (or first) catch of this exception.
              >
              > use:
              >
              > try {
              > // YourException danger
              > }
              > catch(YourExcep tion ex) {
              > // place for logging to text file
              > throw;
              > }
              >
              > Regards
              >
              > Marcin[/color]

              I have class with some public methods. Methods can throw exceptions, but
              I want that user-class can define which exception must be thrown.
              Example:
              public class One
              {
              public void oneFun(Exceptio n e)
              {
              ...
              throw e; <- WRITING IN LOG FILE
              }
              }

              public class Two
              {
              public void twoFun()
              {
              ...
              One one = new One();
              one.oneFun(new LoggedException ()); <- NOT WRITING IN LOG FILE
              ...
              }
              }

              Comment

              • Nicholas Paldino [.NET/C# MVP]

                #8
                Re: Exception throw

                Nenad,

                I think that what you are doing is a bad idea. There are two reasons
                for this. The first is that it is completely possible that your exceptions
                can be caught and handled inside the application boundaries, which
                eliminates the exceptional case. In this case, you would have entries in
                the log that would be frivilous. The second is that other exceptions that
                are not handled inside the application will not log.

                If the case that you want to just log items, you should really use
                something else other than an exception (unless you truly have an exceptional
                condition). Throwing exceptions can have a detrimental effect on
                performance.

                If anything, I would say have exception handlers that exist at the app
                domain boundaries, and then have a general logging mechanism for ANY
                exception that is caught during program execution. If you want to log other
                events outside of exception handling, then have a general log library to do
                that.

                Hope this helps.


                --
                - Nicholas Paldino [.NET/C# MVP]
                - mvp@spam.guard. caspershouse.co m

                "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
                news:Oi0XGmGVEH A.3788@TK2MSFTN GP12.phx.gbl...[color=blue]
                > Leon Lambert wrote:
                >[color=green]
                > > Most people create their own application exceptions instead of using the
                > > generic System.Exceptio n so i will assume you did also. What you could
                > > do is create a Factory method on you exception class and have it create
                > > the exception, log to somewhere then throw the exception. Following is a
                > > short example.
                > > class MyException : Exception{
                > > public MyException(Str ing msg) : base(msg){
                > > }
                > > public static void ExceptionFactor y(String msg){
                > > MyException except = new MyException(msg );
                > > // log it
                > > System.Console. Out.WriteLine(m sg);
                > > throw(except);
                > > }
                > > }
                > >
                > > call it like this.
                > > MyException.Exc eptionFactory(" Test Exception");
                > >
                > >
                > > Just an idea hope it helps.
                > > Leon Lambert
                > >
                > > Nenad Dobrilovic wrote:
                > >[color=darkred]
                > >> Hi,
                > >> Is it possible for exception object to be aware of it's throwing?
                > >> I want to log in the text file when exeption is thrown, not when the
                > >> exception object is created (because I can create exception object
                > >> that is never thrown).
                > >> Thank you,
                > >> Nenad[/color][/color]
                >
                > Thank you,
                >
                > OK, I did it the same way you told me, but just before I read it :)
                > I think it is goog idea.
                >
                > Nenad[/color]


                Comment

                • Nenad Dobrilovic

                  #9
                  Re: Exception throw

                  Nicholas Paldino [.NET/C# MVP] wrote:
                  [color=blue]
                  > Nenad,
                  >
                  > I think that what you are doing is a bad idea. There are two reasons
                  > for this. The first is that it is completely possible that your exceptions
                  > can be caught and handled inside the application boundaries, which
                  > eliminates the exceptional case. In this case, you would have entries in
                  > the log that would be frivilous. The second is that other exceptions that
                  > are not handled inside the application will not log.
                  >
                  > If the case that you want to just log items, you should really use
                  > something else other than an exception (unless you truly have an exceptional
                  > condition). Throwing exceptions can have a detrimental effect on
                  > performance.
                  >
                  > If anything, I would say have exception handlers that exist at the app
                  > domain boundaries, and then have a general logging mechanism for ANY
                  > exception that is caught during program execution. If you want to log other
                  > events outside of exception handling, then have a general log library to do
                  > that.
                  >
                  > Hope this helps.
                  >
                  >[/color]
                  Thank you,
                  My idea was to have exception which is logged (in file) every time when
                  it is thrown, without any other actions.
                  If one has to log exception every time he catch it, it is possible for
                  him to forget to log it. Beside, it is the same code in every catch()
                  block, so it is dull and tiresome.

                  Comment

                  • Nicholas Paldino [.NET/C# MVP]

                    #10
                    Re: Exception throw

                    Nenad,

                    The thing is, why are you logging every exception that is thrown? It's
                    the exceptions that are not caught that are the issue, IMO. If the
                    exception is caught and handled later on in your application, then what's
                    the point of knowing that it was even thrown at all?

                    This is why you have one exception handler at the entry point to your
                    app that will do the logging. That way, you have access to ALL unhandled
                    exceptions, not just your kind (and they are unhandled, which is really the
                    more problematic of the situations).


                    --
                    - Nicholas Paldino [.NET/C# MVP]
                    - mvp@spam.guard. caspershouse.co m

                    "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
                    news:eNYaBKHVEH A.2716@tk2msftn gp13.phx.gbl...[color=blue]
                    > Nicholas Paldino [.NET/C# MVP] wrote:
                    >[color=green]
                    > > Nenad,
                    > >
                    > > I think that what you are doing is a bad idea. There are two[/color][/color]
                    reasons[color=blue][color=green]
                    > > for this. The first is that it is completely possible that your[/color][/color]
                    exceptions[color=blue][color=green]
                    > > can be caught and handled inside the application boundaries, which
                    > > eliminates the exceptional case. In this case, you would have entries[/color][/color]
                    in[color=blue][color=green]
                    > > the log that would be frivilous. The second is that other exceptions[/color][/color]
                    that[color=blue][color=green]
                    > > are not handled inside the application will not log.
                    > >
                    > > If the case that you want to just log items, you should really use
                    > > something else other than an exception (unless you truly have an[/color][/color]
                    exceptional[color=blue][color=green]
                    > > condition). Throwing exceptions can have a detrimental effect on
                    > > performance.
                    > >
                    > > If anything, I would say have exception handlers that exist at the[/color][/color]
                    app[color=blue][color=green]
                    > > domain boundaries, and then have a general logging mechanism for ANY
                    > > exception that is caught during program execution. If you want to log[/color][/color]
                    other[color=blue][color=green]
                    > > events outside of exception handling, then have a general log library to[/color][/color]
                    do[color=blue][color=green]
                    > > that.
                    > >
                    > > Hope this helps.
                    > >
                    > >[/color]
                    > Thank you,
                    > My idea was to have exception which is logged (in file) every time when
                    > it is thrown, without any other actions.
                    > If one has to log exception every time he catch it, it is possible for
                    > him to forget to log it. Beside, it is the same code in every catch()
                    > block, so it is dull and tiresome.[/color]


                    Comment

                    • Nenad Dobrilovic

                      #11
                      Re: Exception throw

                      Nicholas Paldino [.NET/C# MVP] wrote:
                      [color=blue]
                      > Nenad,
                      >
                      > The thing is, why are you logging every exception that is thrown? It's
                      > the exceptions that are not caught that are the issue, IMO. If the
                      > exception is caught and handled later on in your application, then what's
                      > the point of knowing that it was even thrown at all?
                      >
                      > This is why you have one exception handler at the entry point to your
                      > app that will do the logging. That way, you have access to ALL unhandled
                      > exceptions, not just your kind (and they are unhandled, which is really the
                      > more problematic of the situations).
                      >
                      >[/color]
                      OK, I agree. I do not log every exception that is thrown.
                      I have to log every throwning of some kind of exception (I called it
                      LoggedException ), no metter if it is handled or not.
                      I want to overview log file later, so I can see when did my app
                      recovered (or not) of serious error (what was wrong, etc.)

                      Comment

                      • Nicholas Paldino [.NET/C# MVP]

                        #12
                        Re: Exception throw

                        Nenad,

                        If that is the case, the best thing you can do is write to the log when
                        the exception is created. The reason for this is that there is no hook in
                        the framework to allow you to know when an exception is thrown. Since most
                        people do not keep instances of exceptions hanging around in their code, I
                        think it is safe to log when the constructor is thrown.

                        --
                        - Nicholas Paldino [.NET/C# MVP]
                        - mvp@spam.guard. caspershouse.co m

                        "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
                        news:elfsueHVEH A.3280@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > Nicholas Paldino [.NET/C# MVP] wrote:
                        >[color=green]
                        > > Nenad,
                        > >
                        > > The thing is, why are you logging every exception that is thrown?[/color][/color]
                        It's[color=blue][color=green]
                        > > the exceptions that are not caught that are the issue, IMO. If the
                        > > exception is caught and handled later on in your application, then[/color][/color]
                        what's[color=blue][color=green]
                        > > the point of knowing that it was even thrown at all?
                        > >
                        > > This is why you have one exception handler at the entry point to[/color][/color]
                        your[color=blue][color=green]
                        > > app that will do the logging. That way, you have access to ALL[/color][/color]
                        unhandled[color=blue][color=green]
                        > > exceptions, not just your kind (and they are unhandled, which is really[/color][/color]
                        the[color=blue][color=green]
                        > > more problematic of the situations).
                        > >
                        > >[/color]
                        > OK, I agree. I do not log every exception that is thrown.
                        > I have to log every throwning of some kind of exception (I called it
                        > LoggedException ), no metter if it is handled or not.
                        > I want to overview log file later, so I can see when did my app
                        > recovered (or not) of serious error (what was wrong, etc.)[/color]


                        Comment

                        • Nenad Dobrilovic

                          #13
                          Re: Exception throw

                          Nicholas Paldino [.NET/C# MVP] wrote:
                          [color=blue]
                          > Nenad,
                          >
                          > If that is the case, the best thing you can do is write to the log when
                          > the exception is created. The reason for this is that there is no hook in
                          > the framework to allow you to know when an exception is thrown. Since most
                          > people do not keep instances of exceptions hanging around in their code, I
                          > think it is safe to log when the constructor is thrown.
                          >[/color]

                          Yes, I did just like that in the first place.
                          But, sometimes I create exception object without throwing it.

                          Example:

                          public class LoggedException : Exception
                          {
                          LoggedException ()
                          {
                          log();
                          }
                          }

                          public class One
                          {
                          public void oneFun(Exceptio n e)
                          {
                          ...
                          if (..)
                          throw e; <- WRITE IN LOG FILE!
                          ...
                          }
                          }

                          public class Two
                          {
                          public void twoFun()
                          {
                          ...
                          One one = new One();
                          one.oneFun(new LoggedException ()); <- I DONT WANT TO LOG THIS!
                          ...
                          }
                          }

                          So, I had a lot of lines in log which is not actually errors... That is
                          when my drinking problems begun...

                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: Exception throw

                            Nenad,
                            In addition to the other comments:[color=blue]
                            > My idea was to have exception which is logged (in file) every time when
                            > it is thrown, without any other actions.
                            > If one has to log exception every time he catch it, it is possible for
                            > him to forget to log it. Beside, it is the same code in every catch()
                            > block, so it is dull and tiresome.[/color]

                            Depending on the type of application you are creating, .NET has three
                            different global exception handlers.

                            For ASP.NET look at:
                            System.Web.Http Application.Err or event
                            Normally placed in your Global.asax file.

                            For console applications look at:
                            System.AppDomai n.UnhandledExce ption event
                            Use AddHandler in your Sub Main.

                            For Windows Forms look at:
                            System.Windows. Forms.Applicati on.ThreadExcept ion event
                            Use AddHandler in your Sub Main.

                            It can be beneficial to combine the above global handlers in your app, as
                            well as wrap your Sub Main in a try catch itself.

                            There is an article in the June 2004 MSDN Magazine that shows how to
                            implement the global exception handling in .NET that explains why & when you
                            use multiple of the above handlers...

                            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


                            For example: In my Windows Forms apps I would have a handler attached to the
                            Application.Thr eadException event, plus a Try/Catch in my Main. The
                            Try/Catch in Main only catches exceptions if the constructor of the MainForm
                            raises an exception, the Application.Thr eadException handler will catch all
                            uncaught exceptions from any form/control event handlers.

                            Hope this helps
                            Jay


                            "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
                            news:eNYaBKHVEH A.2716@tk2msftn gp13.phx.gbl...[color=blue]
                            > Nicholas Paldino [.NET/C# MVP] wrote:
                            >[color=green]
                            > > Nenad,
                            > >
                            > > I think that what you are doing is a bad idea. There are two[/color][/color]
                            reasons[color=blue][color=green]
                            > > for this. The first is that it is completely possible that your[/color][/color]
                            exceptions[color=blue][color=green]
                            > > can be caught and handled inside the application boundaries, which
                            > > eliminates the exceptional case. In this case, you would have entries[/color][/color]
                            in[color=blue][color=green]
                            > > the log that would be frivilous. The second is that other exceptions[/color][/color]
                            that[color=blue][color=green]
                            > > are not handled inside the application will not log.
                            > >
                            > > If the case that you want to just log items, you should really use
                            > > something else other than an exception (unless you truly have an[/color][/color]
                            exceptional[color=blue][color=green]
                            > > condition). Throwing exceptions can have a detrimental effect on
                            > > performance.
                            > >
                            > > If anything, I would say have exception handlers that exist at the[/color][/color]
                            app[color=blue][color=green]
                            > > domain boundaries, and then have a general logging mechanism for ANY
                            > > exception that is caught during program execution. If you want to log[/color][/color]
                            other[color=blue][color=green]
                            > > events outside of exception handling, then have a general log library to[/color][/color]
                            do[color=blue][color=green]
                            > > that.
                            > >
                            > > Hope this helps.
                            > >
                            > >[/color]
                            > Thank you,
                            > My idea was to have exception which is logged (in file) every time when
                            > it is thrown, without any other actions.
                            > If one has to log exception every time he catch it, it is possible for
                            > him to forget to log it. Beside, it is the same code in every catch()
                            > block, so it is dull and tiresome.[/color]


                            Comment

                            • David Levine

                              #15
                              Re: Exception throw

                              If the exception is a custom exception that you define then you can call the
                              logging code from within the constructor of the exception. Other than that
                              (leaving aside future mechanisms like AOP) you are out of luck.

                              "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
                              news:OqhtUrGVEH A.584@TK2MSFTNG P09.phx.gbl...[color=blue]
                              > David Levine wrote:
                              >[color=green]
                              > > There currently is no means for the exception object itself to detect[/color][/color]
                              when[color=blue][color=green]
                              > > it is thrown, but it is easy for your code to explicitly log it when it
                              > > throws the exception. What are you trying to accomplish?
                              > >
                              > >
                              > > "Nenad Dobrilovic" <cheya1975@hotm ail.com> wrote in message
                              > > news:uhMjaqEVEH A.3024@TK2MSFTN GP09.phx.gbl...
                              > >[color=darkred]
                              > >>Hi,
                              > >>Is it possible for exception object to be aware of it's throwing?
                              > >>I want to log in the text file when exeption is thrown, not when the
                              > >>exception object is created (because I can create exception object that
                              > >>is never thrown).
                              > >>Thank you,
                              > >>Nenad[/color]
                              > >
                              > >
                              > >[/color]
                              >
                              > I want that when someone write:
                              > throw new LoggedException (...);
                              > it is automatically logged in text file.[/color]


                              Comment

                              Working...