Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts regarding exception handling.  RSS feed

 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been using exception handling in my code but still have some doubts regarding exception handling:

1. It is said that exception handling prevents abrupt termination of the program. But itnt the program still terminating abruptly? E.g If I get say an SQLException,

the program control will to to catch block and program will terminate.

2. Is logging the exception, the most common use case of exception handling or are there other common uses also. The majority of catch block I have seen do nothing but

catch and log the error message.

3. If I have an SQLExcepiton Catch block and I log the exception received, then I will be able to determine from logs the SQl exception had occurred and this was its

error message.Instead if I an Exception catch block instead of SQLException catch block, and I log the exception I would still be able to determine from the logs that

SQL exception had occurred and was the error message. So whats the difference?

4. If there are particular type of catch blocks used in the code, is it a good practice to have and Exception catch block too at the end of these catch blocks so that

in any case if particular type is missed, at least it comes in Exception catch block?

5. If I have an application that stores the data in database, then is there any advantage of creating a custom exception for this case?

Thanks.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Monica,

I've tried to answer the questions in the order presented, but I feel some are interlinked. For your reference, I recommend you check out: https://docs.oracle.com/javase/tutorial/essential/exceptions/

1. It is said that exception handling prevents abrupt termination of the program. But isn't the program still terminating abruptly?

2. Is logging the exception, the most common use case of exception handling or are there other common uses also. 

Internally to the program, external connections may need to be closed, and cached data persisted. This is part of the graceful shutdown process. That said, the point of Exception handling is to handle an exception so that the program doesn't need to end. Ideally, the exception handling should allow the program to recover.

SQLExceptions are a great example. Let's say you work for a bank and your job is to create an online application form. You think you have anticipated every possible scenario, but still a user manages to pass garbage data to the database. The column doesn't support that datatype and throws an exception. Now, if you don't handle the exception, your app crashes and your bank cannot process anymore applications.

Or, you handle the exception by:
1. Advising the user something went wrong and to call the tollfree customer service line
2. Compile the invalid data and redirect to another mechanism (maybe a branch person calls the client to clarify the information).
3. Generate a detailed log for the programmers to debug for the future.

3. If I have an SQLException Catch block and I log the exception received...

4. If there are particular type of catch blocks used in the code...


Exception catching should be granular, because the different types of Exceptions generally require different types of handling.

5. If I have an application that stores the data in database, then is there any advantage of creating a custom exception for this case?

Granularity is one big reason. SQLException is kind of a catchall and can often be very difficult to source. For example, in Spring, they have replaced four somewhat generic JDBC related Exceptions with about 35 more specific Exceptions. Everything from DuplicateKeyException to EmptyResultDataAccessException. Some more are listed on this link: http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/dao.html

Another reason to create custom Exceptions is to hide implementation. For example, you could have a DAOIllegalArgumentException that is thrown when the database is unable to persist, because the datatypes don't match, but the same exception could be thrown before the data layer by a validation routine. Users of your class wouldn't need to worry what caused the exception - just that a DAOIllegalArgumentException is thrown when an attempt to persist garbage data occurs.
 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

The below example was very useful to understand the concept:

1. Advising the user something went wrong and to call the tollfree customer service line
2. Compile the invalid data and redirect to another mechanism (maybe a branch person calls the client to clarify the information).
3. Generate a detailed log for the programmers to debug for the future.


I have 1 doubt. I have and application that just stored the data in mysql database.(Another application is supposed to pick this data and show on web).
 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

The below example was very useful to understand the concept:

1. Advising the user something went wrong and to call the tollfree customer service line
2. Compile the invalid data and redirect to another mechanism (maybe a branch person calls the client to clarify the information).
3. Generate a detailed log for the programmers to debug for the future.


I have 1 doubt. I have and application that just stored the data in mysql database.(Another application is supposed to pick this data and show on web).

Is creating custom exceptions of any benfit to this application( The application which pushes data into DB, not the application which displays it on the web).
 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

The below example was very useful to understand the concept:

1. Advising the user something went wrong and to call the tollfree customer service line
2. Compile the invalid data and redirect to another mechanism (maybe a branch person calls the client to clarify the information).
3. Generate a detailed log for the programmers to debug for the future.


I have 1 doubt. I have and application that just stored the data in mysql database.(Another application is supposed to pick this data and show on web).

Is creating custom exceptions of any benefit to this application( The application which pushes data into DB, not the application which displays it on the web).
 
Monica Shiralkar
Ranch Hand
Posts: 919
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I will take care, that edited post does not get reposted. I am trying to understand what does the below mean:

move the discussion to the Ranch Office
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a forum called the Ranch Office.  Your discussion has been moved there.
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...okay, it's not your discussion, but Daniel Cox's.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!