0

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 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 ?

5 Answers 5

1

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

1

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.

4 Comments

the size is allocated on the previous line to this snippet the exact way you advise :)
@Skeith - if your code is right (as you seem to be implying) why is it throwing an exception?
because it did not receive the whole message and this crashed the thread. looking for a way to handle the error and request a resend
@Skeith - if you say so. Frankly I don't believe it, but that's your business, not mine.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.