0

I have conceptual question here for try catch. We use try catch so that exception can be skipped and further code gets executed (*correct me if i am wrong) Point is Exeption is kind of error which is ideally should be resolved or should be handled any which way what is point skipping tht error through "try catch" and executing rest of code. Its like delaying error to resolve.

1 Answer 1

0

The objective of a try catch block is not necessarily to skip an error. The objective is to try and do something which could fail and then catch the exception (e.g., read a file from disk, but the file might not even be there, etc.). After catching an exception, you can handle it.

try {
   riskyOperation();
catch (ExpectedException) {
   handleException();
}

Handling the exception can be of different variations:

  • it could simply be a way to present the error nicely to the end user (replace ugly system error by a user-friendly message)
  • it could store the error in a log somewhere, to be processed later by some person or process
  • it could display extra debugging information in the logs
  • it can be used to roll back a set of transactions to the initial state (e.g. if you do multiple dml statements, make sure they all get undone if only the last one fails)
  • any combination of the above

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .