Results 1 to 5 of 5

Thread: Identify active methods in java

  1. #1
    Join Date
    Mar 2010
    Posts
    222

    Identify active methods in java

    Hello,
    I am very new to java and I have a query regarding this. If my program encounters a division by zero stops and tells me he very precisely where the error occurred, not just the object and method, but all active methods that led to the error.
    Here is what I get
    Code:
    Exception in thread "Hand" java.lang.ArithmeticException: / By zero
            Has CommoditiesMarket.updateOffer(CommoditiesMarket.Java:141)
            Has Store.sell(Store.Java:65)
            Has Firm.sell(Firm.Java:237)
            Did Household.purchase(Household.Java:62)
            Did Household.Spend(Household.Java:118)
            Has HouseholdsSector.Spend(HouseholdsSector.Java:81)
            Has Test.doPeriod(Test.Java:23)
            Has Test.hand(Test.Java:58)
    Can you please explain me this error, or sort this error for me. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Identify active methods in java

    Hello,
    To catch the exception it must use the try / catch blocks. In your case:
    Code:
    try {
    	int x = 100/0;
    } wrestling (ArithmeticException e) {
    	System.out.println("Error");
    }
    You have not used try and catch block in your code, please use this you will find the output accordingly. That is the result will make a difference when you are going to use the try and catch block in your program.

  3. #3
    Join Date
    Nov 2009
    Posts
    356

    Re: Identify active methods in java

    Hello,
    To summarize how in a method to retrieve the name (and possibly line) method (or all methods) active and leading to the execution of the current method. I've never done this, but it's possible you should look at the Javadoc and the Throwable class and StackTraceElement. I would not want to say stupid things but I think you can use the method:
    Code:
    public StackTraceElement[] getStackTrace()
    I hope this will help you and you will find a way out of this.

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Identify active methods in java

    Hello,
    Take a look at the following example, this may help you.
    Here is the code
    Code:
    private void printErr(Throwable t, String errTitle) {
    
        StackTraceElement[] stctrc = t.getStackTrace();
        StringBuffer msg = New StringBuffer(tgetClass().getSimpleName());
        msg.append(" : ");
        msg.append(stctrc[0].function toString() {
        [native code]
    }());
        msg.append('\ N');
        msg.append("  --> ");
        msg.append(tgetMessage());
        System.err.println(msg.function toString() {
        [native code]
    }());
    
        joptpn.showMessageDialog(null,
            errTitle + "\ N" + TgetClass().getSimpleName() + " : " + TgetMessage(),
            "Error" + TgetClass().getSimpleName(),
            joptpn.Error_message);
      }

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Identify active methods in java

    Hello,
    Otherwise from Java 5.0 can use directly the methods
    Code:
    getStackTrace()
    Thread class
    Code:
    StackTraceElement[] stctrc = Thread.currentThread().getStackTrace();
    But be careful because whatever the method used, the generation of the call stack is fairly expensive and must not be misused under penalty of stopping in your code. If you have any more queries regarding this topic then please let me know. Thanks in advance.

Similar Threads

  1. What is Export Methods in Java?
    By NIcaBoy in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 06:29 AM
  2. Native methods of java
    By Truster in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 10:52 AM
  3. join() and isAlive() methods of java
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 08:28 AM
  4. Methods in JAVA
    By Caiden in forum Software Development
    Replies: 3
    Last Post: 18-08-2009, 11:26 PM
  5. Identify Active directory topology
    By bynodesign in forum Operating Systems
    Replies: 2
    Last Post: 03-02-2009, 08:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,791,328.24117 seconds with 16 queries