• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Question about throwing exceptions

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 97
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, that clears things up quite a bit. Thank you for all the responses and the link : )
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic