Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Counting number of exceptions in a Java class

Thread view: 
Manu Mahendru - 09 Jan 2006 18:04 GMT
Hi!

I need to count the number of exceptions in a (set of) Java source
files.

Could anyone give me a lead on this? I have had a look at the
checkstyle plugin (Maven2). Am not too familiar with it yet... and it
seems it may not be the best way forward for a solution. I was also
wondering if we could change the Javadoc generation source to help
produce the final count.

Any ideas/suggestions would greatly help.

Thank you!

Manu
Abhijat Vatsyayan - 09 Jan 2006 18:59 GMT
Do you mean  - find all classes in a source code base that inherit from
java.lang.Exception?
You can use write custom doclet to do this. See method "superclass()" in
class "com.sun.javadoc.ClassDoc" .

If you can compile the source and have a list of all classes, you can
also use reflection (see method isAssignableFrom(...) in class
java.lang.Class).

Abhijat

> Hi!
>
[quoted text clipped - 12 lines]
>
> Manu
Roedy Green - 09 Jan 2006 22:14 GMT
>I need to count the number of exceptions in a (set of) Java source
>files.

Just what do you mean my that?

You could gather all the import statements, sort them, and count how
many were of each flavour and toss the classes that did not contain
the word "exception" or "error" somewhere in the name.

I don't know why that metric would be meaningful.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Manu Mahendru - 10 Jan 2006 17:48 GMT
Hmmmm.... I should have put it better.

What I meant was that I need to count the total number of exceptions
thrown by the methods in all classes in a project - they may be caught
some place else... but if they are being thrown... I need to count
them.

I was thinking of javadocs... but thot checkstyle would be better. Will
have a look at both.

As for the usefullness of the metric... in my case it does seem to be a
worthwhile figure to have.

Thanks for the reply!

Manu
Eric Sosman - 10 Jan 2006 18:00 GMT
Manu Mahendru wrote On 01/10/06 12:48,:
> Hmmmm.... I should have put it better.
>
[quoted text clipped - 8 lines]
> As for the usefullness of the metric... in my case it does seem to be a
> worthwhile figure to have.

   I think you still have a specification issue.
For example, in

    void foo()
       throws SocketException, ProtocolException
    { ... }

    void bar()
       throws IOException
    { ... }

how many exceptions do you want to count for each method?
(Note that bar() can throw anything foo() can, and more.)

Signature

Eric.Sosman@sun.com

Oliver Wong - 10 Jan 2006 19:54 GMT
> Manu Mahendru wrote On 01/10/06 12:48,:
>> Hmmmm.... I should have put it better.
[quoted text clipped - 23 lines]
> how many exceptions do you want to count for each method?
> (Note that bar() can throw anything foo() can, and more.)

   As a naive first attempt at the problem, which may be good enough for
the OP, you could just do a plain text search for the string "throw" in all
your Java source files, and count how many times they occur. You can then
refine this solution to ignore matches found inside of strings and comments.
I believe this can easily be done with a simple DFA, so in practice I'd use
an regular expression engine if one were already available to me, or I'd
write a quick and dirty DFA engine to process the text files for me.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.