8 Replies Latest reply: Sep 30, 2010 1:47 AM by EJP RSS

    Detecting when exception was thrown using custom class loader

    801565
      Hello all,

      I would like to implement the solution described here - http://stackoverflow.com/questions/75218/how-can-i-detect-when-an-exceptions-been-thrown-globally-in-java - that uses custom class loader in order to detect when an Exeption thrown somewhere in the JVM hosting my app, please note that exceptions might be thrown from 3rd party jars the app is using. So, thanks to help I got from another post, I've managed to code the custom class loader. My question is how can the class loader wrap the original exception, as the methods in ClassLoader deals with classes, not instances. So where should I set the original exception?

      Thanks!

      Edited by: user9355666 on Sep 28, 2010 10:48 PM
        • 1. Re: Detecting when exception was thrown using custom class loader
          801580
          According to my information a custom classloader will help you only in handling class loading exceptions, and not all exceptions thrown by the loaded classes. You should try the chosen answer which hooks onto the thread and catches all uncaught exceptions. All The Best.

          Bridal Shower Favors
          wedding favors

          Edited by: 798577 on Sep 28, 2010 11:34 PM
          • 2. Re: Detecting when exception was thrown using custom class loader
            801565
            I'm quite new to class loader and trying to practice :-)

            Thanks for the answer, but I would like 2 catch all exceptions thrown, caught or auncaught, by my code or 3rd party jars (these 3rd party may even spawn threads that I can't hook). Is it impossible?

            Thanks.
            • 3. Re: Detecting when exception was thrown using custom class loader
              EJP
              Read the thread you cited. It tells you.
              • 4. Re: Detecting when exception was thrown using custom class loader
                801565
                Well, I did read... :-)

                The problem I'm facing are as follow:

                1. An exception might be thrown from different places (for example NullPointerException), but the class is loaded only once (Am I right?), so how this should be handled with classloader?
                2. When subclassing classloader, the methods deal with loading classes, not instances, so how the exception wrapper can be instanciated to wrap the thrown exception?

                Thanks for any help!
                • 5. Re: Detecting when exception was thrown using custom class loader
                  EJP
                  Strange that you can't read your own citation. It says there quite clearly to intercept then class-loading of the exception classes and substitute your own at that point.*
                  • 6. Re: Detecting when exception was thrown using custom class loader
                    801565
                    I think I'm missing something fundumental, forgive me for being slow...

                    This is what I did so far. For the exception wrapper I made a simple class extens Exception that recieve Exception in its ctor and store it. I also subclassed ClassLoader and override its loadClass(). I've registered it as the system classloader. My thinking was to check in that point that if the requested class is instance of Exception and if yes, returning my wrapper class wrapping this exception. But, since loadClass() return class, how can I set in the wrapper the original exception?

                    In addition, let's say 2 different places in the code throws NPE, to my understanding the classloader will load NPE only once, so how throwing the NPE in the second time can be detected?

                    Thanks again for the guiding, and for the willing to help!
                    • 7. Re: Detecting when exception was thrown using custom class loader
                      jtahlborn
                      user9355666 wrote:
                      I think I'm missing something fundumental, forgive me for being slow...

                      This is what I did so far. For the exception wrapper I made a simple class extens Exception that recieve Exception in its ctor and store it. I also subclassed ClassLoader and override its loadClass(). I've registered it as the system classloader. My thinking was to check in that point that if the requested class is instance of Exception and if yes, returning my wrapper class wrapping this exception. But, since loadClass() return class, how can I set in the wrapper the original exception?

                      In addition, let's say 2 different places in the code throws NPE, to my understanding the classloader will load NPE only once, so how throwing the NPE in the second time can be detected?
                      you are missing a key point. you should creating a custom implementation of the NPE class which hooks into your detection code in its constructor. from that point forward, anytime any NPE (which is your custom class) is constructed, you can detect it.
                      • 8. Re: Detecting when exception was thrown using custom class loader
                        EJP
                        I think I'm missing something fundumental, forgive me for being slow...
                        Me too. Why are you asking here about something in another thread on another site?

                        I'm locking this.