File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Try catch

 
Vasantha Prabha
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam having a question on try/catch
When Iam using a try /catch
How should I find out which line has thrown the exception ??
Regards,
Sangeetha Prabindh
 
Amit KumarS
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiii..
this is pretty simple.. in my view there are two ways to do this.. follow whichever suits u..
1. we can write a System.out.println() after each line.. so that it will print all SOPs before the line which has thrown exception. and no SOPs after that line.. so u can figure out which line is throwing exception.
here is a sample code for that.

the out put of this will be

***** 1
***** 2
******** Here in Exception *********

2. the second way is even easier... just call printStackTrace() in the catch block.. it will print out the Line Number along with the cause of the error.
example is : -

the output for this will be

java.lang.ArithmeticException: / by zero
at test.main(test.java:7)

i Hope i have answered your question.. if still in doubt, please get back...
Thnx
Amit
 
Vasantha Prabha
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Amit realy it is helpful
sangeetha prabindh
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a little touch on Amit's 2nd approach, under SDK 1.4, you can use e.getStackTrace() to get StackTraceElement[], it provides several methods
getClassName(), getMethodName(), getLineNumber(), you can use them to customize the result.
eg.
StackTraceElement[] stk = e.getStackTrace();
String className = stk[0].getClassName();
String methodName = stk[0].getMethodName();
String lineNumber = stk[0].getLineNumber();
Originally posted by Amit KumarSingh:

2. the second way is even easier... just call printStackTrace() in the catch block.. it will print out the Line Number along with the cause of the error.
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add one other point. As a general rule, your program does not usually need to find out what line of code generated an exception. Rather, it usually needs to know what type of error occurred.
In Java's exception handling mechanism, the type of the error is specified by the type of the exception that is thrown For example, if an IO error occurs, then an IOException is thrown, if division-by-zero is attempted, an ArithmeticException is thrown, if an array boundary is exceeded, an ArrayIndexOutOfBoundsException is generated, and so on.
Thus, determining at runtime which line of code generated the exception is a specialized technique that might be useful when debugging code, but would not usually be used to handle exceptions in general.
 
Jacquie Barker
author
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To follow up on Herb's posting, the smaller your "try" block, and the more specific your "catch" clauses are, the more tightly you'll be able to trap exceptions, and hence will have a better chance at recovering from them. At the other extreme, if you have a HUGE try block with a single
catch (Exception e) { ... }
catch block at the end, there'll be SO MANY different possible ways of getting into that catch block that the job of sorting out what went wrong so as to handle the situation is almost impossible.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic