how to error handle java code
I am new to java and and trying to test/improve my program.
it works on my computer but not on the clients so I and trying to do error handling but am not sure about the syntax.
How do you put try/catch on for and开发者_StackOverflow中文版 do while loops as well as if statements. would it just go inside them like any other command ?
for(int eight = 0; eight < 8; eight++)
{
message = in.readLine();
LOGGER.fatal(eight);
LOGGER.fatal(message);
if(message.contains("Content-Length"))
{
message.getChars(16, 20, size, 0); THIS LINE
}
}
this is a an example of what I am using. how do I add a try/catch to the marked line.
Or do you have a better way to handle the exceptions ?
how do I add a try/catch to the marked line.
If you don't want to break out of the loop upon an exception you encapsulate only the marked line in the try / catch.
for(int eight = 0; eight < 8; eight++) {
message = in.readLine();
LOGGER.fatal(eight);
LOGGER.fatal(message);
if(message.contains("Content-Length")) {
try {
message.getChars(16, 20, size, 0); THIS LINE
} catch (YourException e) {
// Handle the exception
}
}
}
Simply do this.
for(int eight = 0; eight < 8; eight++)
{
message = in.readLine();
LOGGER.fatal(eight);
LOGGER.fatal(message);
if(message.contains("Content-Length"))
{
try {
message.getChars(16, 20, size, 0);
}
catch(Exception e) {
// Print all exception messages here ...
}
}
}
Remember : Always try to use specific exception class inside the catch instead of 'Exception' as a good programming practise.
Where your try/catch lines go depends on what exactly you want to handle.
You may want to handle all exceptions thrown by an entire method in the same way (such as passing a more readable version to some output and/or passing a meaningful error message to the user) - in this case, you'd do something like this:
public void method() {
try {
// entire method code here
}
catch (Exception e) {
// handle exception here
}
}
Alternatively, you may want to handle an exception thrown by a particular line (such as handling a specific exception thrown when opening a file), in which case you'd surround only that one line with your try/catch statements.
The simple answer to your question is:
try {
for (int eight = 0; eight < 8; eight++) {
message = in.readLine();
if (message.contains("Content-Length")) {
message.getChars(16, 20, size, 0); THIS LINE
}
}
} catch (StringIndexOutOfBoundsException ex) {
Logger.fatal("I've got a bug in my program", ex);
}
A better answer is that you need to fix the cause of the exceptions. I bet it is the code that attempts to extract the content length is making invalid assumptions about the number of characters there are in the "size" field of an input line. A more robust approach is something like this:
int pos = message.indexOf("Content-Length ");
if (pos >= 0) {
String size = message.substring(pos + "Content-Length ".length());
}
Obviously, this allocates an extra String, but it has the advantage that IT WORKS.
You simply surround the line(s) which might throw an Exception with a try
-block and catch the thrown Exception in a catch
-Block. See the Java Tutorial
精彩评论