4 Replies - 217 Views - Last Post: 17 January 2013 - 06:26 AM Rate Topic: -----

#1 tieugiang94  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 160
  • Joined: 07-December 11

When should we use Exception ?

Posted 17 January 2013 - 03:34 AM

I leanred exceptions are used to catch errors (such as: files, network, user input ).
Do we use it for every case ( listed above ) ?

For example devide for 0. We can use IF statment to check a variable then show a message to user instead of use exception ?
Or, working with files, we can check is it exists or not to avoid error ?

I don't know when should we catch error by our code ( ex: if, while..v.) or use Exception.

So, which situation should we use Exception ?

This post has been edited by tieugiang94: 17 January 2013 - 03:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: When should we use Exception ?

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: When should we use Exception ?

Posted 17 January 2013 - 04:02 AM

Well, take a look at this link and hopefully, you will find what you are looking for.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: When should we use Exception ?

Posted 17 January 2013 - 04:51 AM

Exceptions are throwed when a method is in front a situation it can't handled to inform the method that called it that something went wrong

Have a look at Integer.parseInt() that can't throw a NumberFormatException
When Integer.parseInt() which tries to translate a String to an int encounters an invalid character what should it do ? It can't return -1 (as a flag saying It didn't work) because -1 is a valid int that can be contained in the String it translates so ? It throws an Exception which basically means "you put me in a situation I can't handle".

So in one phrase: a method throws an Exception when it is in a situation it can't handle.
Was This Post Helpful? 4
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: When should we use Exception ?

Posted 17 January 2013 - 05:33 AM

The link that was posted above pretty much sums the nature of exceptions. In regards to your question about using them, it's really an acquired thing. After programming for some time and seeing the types of exceptions that are commonly thrown, especially when working in a framework or a with a database or something, you'll get a hunch about the types of error conditions you need to handle. As a best practice, if some code throws an exception you should handle it and do so in a way that's actually helpful. Don't just leave an empty catch block. Print the stack trace or log the exception, and if possible recover. It can save you a lot of time in the end.

For example devide for 0. We can use IF statment to check a variable then show a message to user instead of use exception ?
Or, working with files, we can check is it exists or not to avoid error ?



Well, if you're writing code that will have a client, it's best to throw an exception, since division by 0 is an exception (and Java treats it as one). If you don't throw an exception and the divisor is a 0, what will you return? Like I said, this really only applies to writing a method that someone else will use. If you were say... writing a calculator, and the user inputs a 0, you could simply display an error message and keep it moving. In other words, as pbl said, if you put me in a situation where I can't do proceed, throw an exception, otherwise keep going.

But back to the method situation. You could let the unchecked exception propagate to the calling code, but that's not a good idea. You need to document what your code does, so the client knows what to expect. If you simply do nothing and the client does nothing, the program could crash. If you specify that an exception could be thrown, at least the client knows (although he may not be forced to handle it). Here's an example of how you could handle a potential divide-by-zero error.

Spoiler

I didn't throw a checked exception, but at least the client is aware of the method's behavior.

This post has been edited by blackcompe: 17 January 2013 - 05:40 AM

Was This Post Helpful? 2
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,106
  • Joined: 19-March 11

Re: When should we use Exception ?

Posted 17 January 2013 - 06:26 AM

Everything said so far in this topic has been excellent, I just wanted to emphasize this point:

View Posttieugiang94, on 17 January 2013 - 05:34 AM, said:

For example devide for 0. We can use IF statment to check a variable then show a message to user instead of use exception ?
Or, working with files, we can check is it exists or not to avoid error ?



Exceptions are not used to communicate with the user. They are used to communicate with the rest of the program. An exception is a method's way of say to the the method that called it "something has happened that I can't cope with."

The idea is that the calling method will have a better idea of what to do than the acting method. It's sort of like when you're on the phone to tech support and they have to stop and get their manager.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1