Srinath Murugula
Srinath Murugula

Reputation: 582

exception handling related to try and catch

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;
}

Upvotes: 2

Views: 124

Answers (4)

cнŝdk
cнŝdk

Reputation: 32145

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.

Upvotes: 0

Md. Rajib Hossain
Md. Rajib Hossain

Reputation: 1

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
      }

Upvotes: 0

techPackets
techPackets

Reputation: 4506

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.

Upvotes: 0

Tom Jonckheere
Tom Jonckheere

Reputation: 1648

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.

Upvotes: 2

Related Questions