my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Question on Try catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on Try catch" Watch "Question on Try catch" New topic
Author

Question on Try catch

Vasantha Prabha
Ranch Hand

Joined: Oct 02, 2003
Posts: 108
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


Regards,Vasantha<p>Great minds discuss ideas. Average minds discuss events. Small minds discuss people.
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
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


****************************<br />In 24 hrs Earth rotates once on its Axis.
Vasantha Prabha
Ranch Hand

Joined: Oct 02, 2003
Posts: 108
Thanks Amit realy it is helpful
sangeetha prabindh
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
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.


not so smart guy still curious to learn new stuff every now and then
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
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.


Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
 
GeeCON Prague 2014
 
subject: Question on Try catch