Counting number of exceptions in a Java class

M

Manu Mahendru

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
 
A

Abhijat Vatsyayan

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
 
R

Roedy Green

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.
 
M

Manu Mahendru

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
 
E

Eric Sosman

Manu Mahendru wrote On 01/10/06 12:48,:
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.

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.)
 
O

Oliver Wong

Eric Sosman said:
Manu Mahendru wrote On 01/10/06 12:48,:

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.)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top