Is there an easy way to find out if the StackTraceElement isn't source code?

Here is a sample application that throws an error in Foo.main()

:

public class Foo {

    public static void main(String args[]) {
        try {
            throw new RuntimeException();
        } catch (Exception e) {
            for (StackTraceElement elem : e.getStackTrace())
                System.out.println(elem);
            System.exit(-1);
        }
    }
}

      

Here's the output:

Foo.main(Foo.java:5)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

      

Let's say I don't want the last five objects StackTraceElement

.

Is there an easy way to filter out the ones that don't compile in my project?

+3


source to share


1 answer


extract from comments (duffymo and mine)



  • Choose a unique name for the top-level package . An example would be
    com.yourcompany

    either com.productname

    ... etc.

  • In the exception handling procedure, check if the package starts with your template.

+1


source







All Articles