PDA

View Full Version : Exceptions in java


kishorekumar
Oct 26, 2006, 02:08 AM
HI,
I have a java class in which an exception occurred. Now the problem is I have to know from which class and from which method the exception is thrown.

LTheobald
Oct 30, 2006, 10:07 AM
Well you normally just have to look at the stack trace and this will tell you where it is thrown. Here's an example:


try {
// The below throws an exception
MyClass.doSomething();
} catch (Exception e) {
System.err.println("Error: "+ e.getMessage());
e.printStackTrace();
}


This will print out something like the following when an error is thrown:


Error: null
java.lang.NullPointerException
at com.myPackage.test.java:25)
at com.myPackage.test.java:18)
at com.myPackage.test.java:14)


The "at com.myPackage.test.java" part is the class that threw the error. The number following the colon is the line error the error was thrown on.