Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How do I log exception when it is thrown

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default How do I log exception when it is thrown

    Here is the situation. When I throw an exception, I want my wrapper class to pick it up and put it into a database. (For simplicity I am just printing it in this example).
    However, when I throw the exception, the constructor for my exception subclass never gets called.

    public class SmartHomeXcp extends Exception
    {
        private final StackTraceElement trace[]; 
        private int sizeOfTrace;
        public SmartHomeXcp(String message)
        {
            super(message);
            trace = getStackTrace();
            sizeOfTrace = trace.length;
            printSize();
        }
     
        public void printSize()
        {
            System.out.println("Size " + sizeOfTrace);
            for(int i = 0; i < sizeOfTrace; i++)
            System.out.println("Trace: " + trace[0]);
            System.out.println("Did we make it here?");
        }
     
    }


    public class ExceptionGiver 
    {
        public static void causeException()
        {
             throw new SmartHomeXcp("Dude exception here!!!!");
        }
     
        public static void main(String[] args)
        {
            ExceptionGiver.causeException();
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I log exception when it is thrown

    However, when I throw the exception, the constructor for my exception subclass never gets called.
    I'm not sure what you mean - the code you posted won't even compile (Exceptions must be caught or thrown).

    When I throw an exception, I want my wrapper class to pick it up and put it into a database.
    If you wish to log exceptions, you might consider looking into a logging API such as Apache log4j. You can use a FileAppender or JDBCAppender to write to a file or database, respectively, without the need for a wrapper class.

  3. The Following User Says Thank You to copeg For This Useful Post:

    huntorotron (March 26th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How do I log exception when it is thrown

    Oh wow is it a compile error???
    I did not even notice that thank you... I thought it was the exception that I was getting in the red letters

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: How do I log exception when it is thrown

    when you throw an Exception, I think you should also handle it, it is either you catch it or you throws it beside the method.
    example:
    public void doExceptions() throws Excetion {
         throw new Exception("balh blah blah");
    }
    or
    public void doException() {
         try {
                throw new Exception("blah blah blah");
         } cathc (Exception e) {
                // do something here
         }
    }

Similar Threads

  1. Socket closed Exception being thrown.. please help
    By adi0011 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 11th, 2014, 06:53 AM
  2. [SOLVED] Copying a file to an array and getting exception thrown for input
    By KristopherP in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 1st, 2013, 12:07 AM
  3. Which of the subclasses of IOException are more probably to be thrown?
    By Kate123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2013, 07:25 AM
  4. Exception Thrown
    By Frame in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 11:14 PM
  5. caught or declare might be thrown error
    By IDK12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 28th, 2011, 01:55 PM