2

Can we write any implementation code in catch block? What are the rules to be used to implement in catch block?

try{
    resultado = (T) mensaje.getBody(clase);

}
catch(Exception ex){
    resultado = null;
    this.destruye();
    throw ex;
}
5
  • Basically, you have to keep in mind that even code in a catch block can throw an Exception, but you're free to handle your exception the way you see fit. Commented May 27, 2015 at 7:51
  • but catch block is only for throwing exception,i think so. Commented May 27, 2015 at 7:55
  • 3
    ehm ... No. a catch block is for 'handling the exception'. if you are just going to re-throw the exception, remove the try-catch block, and add a throws clause to your method's signature. Does the other code in your catch block have an impact on what happens outside of that method? Commented May 27, 2015 at 7:56
  • ya thank you for your answer i agree with you. but whether it impacts any java standard or raises any maintainability issue in doing so. Commented May 27, 2015 at 8:01
  • my point was, if this code: resultado = null; this.destruye(); has no external impact, you should remove the try - catch and just add a throws Exception, since you're not actually handling it. Commented May 27, 2015 at 8:04

4 Answers 4

2

You can write all the code you want in your catch block.

Exception handlers can do more than just print error messages or halt the program. They can do error recovery, prompt the user to make a decision, or propagate the error up to a higher-level handler using chained exception

Remember, this code will only be executed if an exception is thrown.

Sign up to request clarification or add additional context in comments.

1 Comment

ya thank you for your answer i agree with you. but whether it impacts any java standard or raises any maintainability issue in doing so.
0

Yes you can write any code you want in the catch block, as you can see in the catch Blocks Documentation:

The catch block contains code that is executed if and when the exception handler is invoked.

So here the catch block is only executed if the code inside the try block raises an exception, so in that case you can handle the Exception and write whatever code you want.

For example you can write:

Int input = 0;
try {
    System.out.println("Enter a number :");
    input = scanner.nextInt();
} catch (IOException e) {
    System.err.println("Caught IOException: " + e.getMessage());
    System.out.println("Rewrite the number please");
}

And to answer you question about "What are the rules to be used to implement in catch block?" and if it's a bad practice to write code in the catch block:

You can see in the documentation that:

Exception handlers can do more than just print error messages or halt the program. They can do error recovery, prompt the user to make a decision, or propagate the error up to a higher-level handler using chained exceptions, as described in the Chained Exceptions section.

So it's fine to write whatever code you need to write in the catch block, you can take a look at Exception Chaining to see that you can write code there, but keep in mind that it's made to write code that handles the given Exception.

2 Comments

should every method in java handle exception?
@smith no not every method should handle exceptions, it depends on your needs and on the code you wrote, here are some links that may give you further information about it Best Practices for Exception Handling and Advantages of Exceptions.
0

Can we write any implementation code in catch block?

You can write whatever code you want in your catch block. The code will only be executed only when the exception is thrown.

What are the rules to be used to implement in catch block?

Ideally catch blocks contain code which deal with the exceptions for an instance printing stack trace, log the exception, forwarding the flow to a jsp or a method, wrapping the exception & rethrowing it, exit gracefully. Besides this you can write whatever code as per your requirement you don't have to follow any specific rules.

Comments

0

Yes, you can write whatever you want....

If you think that your code porting may be arise any error for that reason the program have terminated in that case to handle that exception you have to used catch block so that the program will not terminate.

try{
        /h/ere code arise exception    
  }
catch(this is place where Exeption class which hold the exception type throw object){
         //here for handling the exception
      }

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.