• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about throwing exceptions  RSS feed

 
Kody Wright
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello : )

I am currently learning about exception handling in my java class and have a question that I can't seem to find a clear answer on. I'm sure that the answer is something very simple that I am somehow overlooking, otherwise it wouldn't be included as part of the language.

I can't seem to understand the purpose of throwing an exception. In the programs I've been dealing with, the catch block automatically catches the exception if the try block doesn't work...so why would I need to throw an exception if the catch block will automatically catch the exception anyway?
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create and throw your own types of exceptions. This is useful if you need to represent application specific error conditions.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, once you've caught an exception, you might want to re-throw it, or perhaps throw a new one.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kody Wright wrote:...why would I need to throw an exception if the catch block will automatically catch the exception anyway?

One thing to consider is that try/catch blocks allow you to separate unusual circumstances (exceptions) from the rest of your code. Throwing an exception is a way of saying, "I need to break out of the regular process because something unusual happened."

Another thing to consider is that catch statements can be very specific in the types of exceptions they catch. You might be used to seeing catch(Exception e), which will catch any type of Exception (including its subtypes). This might have you thinking that all exceptions end up getting caught (handled) in the same place. But, for example, catch(IOException e) will only catch an IOException (and its subtypes). So being more specific in the type of Exceptions caught allows your code to throw different types of exceptions to be handled differently in different catch blocks.

For more information and examples, see Java Tutorial - Advantages of Exceptions.
 
Gaurav Sagar
Ranch Hand
Posts: 97
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are situations where you have to create new exceptions specific to your application. In those cases, you simply extend the Exception class and create your custom exception. Furthermore, you may find a situation where your application could crash due to some known reason, in that case you could throw an exception that encapsulates that problem and handle it.

Regards,
Gaurav Sagar
 
Kody Wright
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, that clears things up quite a bit. Thank you for all the responses and the link : )
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!