Help writing error handler

Hi,

I am a newbie to Java and need some help finding an error.

I run a testing and support website which has been handed over to me with little java training and knowlegde.

Anyway the problem is that there a section where you can attach files, this works fine when saving an issue the first time, but when trying to update/modify issue with more files, it doesnt work ..(no error, etc).

What i need to know is how to write a error handle to capture what part of the script,code is not working.

Not sure if this is the right code but may help;

uatregister.Attachment[] attachments = issue.get_file_attachments();

for(int i=1; i<(attachments.length+1); i++)

attachments[i-1].set_hard_file_name(issue.get_hie_issue_no() + "_evid_" + i + attachments[i-1].get_soft_file_name().substring(attachments[i-1].get_soft_file_name().indexOf("."), attachments[i-1].get_soft_file_name().length()));

database.Query[] queries = issue.get_attachment_sql();

for(int i=0; i<queries.length; i++)

db.update(queries, formPreparedByContact);

String filesAttached = request.getParameter("filesAttached");

String filename;

if(!filesAttached.equals("No Files Attached"))

{

attachments = issue.get_file_attachments();

java.util.StringTokenizer tokens = new java.util.StringTokenizer(filesAttached, "\n", false);

while(tokens.hasMoreTokens())

{

filename = tokens.nextToken();

for(int i=0; i><attachments.length; i++)

{

if(attachments.get_soft_file_name().equals(filename.substring(0, (filename.length()-1))))

{

try

{

Runtime.getRuntime().exec("move \"" + uatregister.Attachment.EVIDENCE_LOCATION + "temp\\" + filename.substring(0, (filename.length()-1)) + "\" \"" + uatregister.Attachment.EVIDENCE_LOCATION + "" + attachments.get_hard_file_name() + "\"");

}

catch(Exception e)

{

new ViewableException(e);

}

}

}

}

}

else

{

try

{

Runtime.getRuntime().exec("rm " + uatregister.Attachment.EVIDENCE_LOCATION + "temp\\*.*");

}

catch(Exception e)

{

}

}

response.sendRedirect("viewUATIssue.jsp?testplanid="+request.getParameter("testplanid")+"&test="+request.getParameter("test")+"&testplanissueno="+request.getParameter("testplanissueno")+"&issueno="+request.getParameter("issueno")+"&releaseid="+request.getParameter("releaseid"));

}

else

Any assistance would be greatly apprecated>

[2613 byte] By [jettimona] at [2007-11-26 19:06:13]
# 1

1. Please use the code formating tags when you post code so that it is easier to read.

Your right, there probably is an error and the code is not showing or logging it.

> What i need to know is how to write a error handle to

> capture what part of the script,code is not working.

Errors have to be handled in java. However, your problem is not that the code doesn't handle it, it is the way it handles it.

try catch statements can be used to handle errors. Here is a case where one exists in your code.

>try

>{

> Runtime.getRuntime().exec("move \"" +

> uatregister.Attachment.EVIDENCE_LOCATION + "temp\\"

> + filename.substring(0, (filename.length()-1)) + "\"

> \"" + uatregister.Attachment.EVIDENCE_LOCATION + ""

> + attachments.get_hard_file_name() + "\"");

>}

> catch(Exception e)

>{

>new ViewableException(e);

> }

> }

I am not sure what putting the Exception in a new "ViewableException" does, but I assume it displays it to the user? I would investigate this.

Here is another place that the code catches an exception. However, rather than doing something with it, it is ignored. I would change that to at least output a message to a console or a log file or maybe even display a message someone? What ever it is, I am not sure if you want to ignore this.

>try

>{

> Runtime.getRuntime().exec("rm " +

> uatregister.Attachment.EVIDENCE_LOCATION +

> "temp\\*.*");

>}

> catch(Exception e)

>{

> }

>}

zadoka at 2007-7-9 20:57:16 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Thanks, How would i write this to a log file..
jettimona at 2007-7-9 20:57:16 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

> Thanks, How would i write this to a log file..

It depends on what you wanted to see. You could simply write out text to a file, or setup something like log4j.There are some tutorials if you search around that can guide you through the setup.

But if you just care about this one case it might be easier to just open a file and write it out.

zadoka at 2007-7-9 20:57:16 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...