Hi guys just wanting some help with exceptions. I understand the try and catch concept but I struggle more with the throw and throws concept, it is my understanding (that may be incorrect), that you a method can be like

public class thisMethod throws whateverException

Am I right in thinking that 'whateverException' is a written exception class? Now I dont understand any further than this or why it would be done.

And what is the purpose of the throw and catch idea? is it that you want the error to be controlled in the right class of your code?

Thanks in advanc e guys

Hi guys just wanting some help with exceptions. I understand the try and catch concept but I struggle more with the throw and throws concept, it is my understanding (that may be incorrect), that you a method can be like

public class thisMethod throws whateverException

Am I right in thinking that 'whateverException' is a written exception class? Now I dont understand any further than this or why it would be done.

And what is the purpose of the throw and catch idea? is it that you want the error to be controlled in the right class of your code?

Thanks in advanc e guys

"whateverException" is any class, whether it be from a standard Java library or a class that you yourself created, that extends Exception. So in some hierarchy, it must extend Exception in order to use the throws keyword. To help you understand why you'd want to declare a method like that, consider the following example: the Java Integer class's parseInt method is declared as 'throws Exception (of some sort)' because it allows the programmer calling the method to catch the Exception and deal with it however they see fit. So it enables the programmer who calls the method to deal with the Exception, if one occurs, and respond appropriately.

Click to see the Integer class's parseInt method.

So essentially, declaring, in the method header, that a method throws an Exception, forces the programmer calling the method to deal with an Exception if one occurs. This puts the control/the decision of what to do (if some Exception occurs) in the hands of the programmer who is calling the method.


edit: Actually, it must extend Throwable (which Exception does) but mostly Exception is used. You can read the Java Sun explanation if you want more info.

Edited 6 Years Ago by BestJewSinceJC: n/a

So essentially, declaring, in the method header, that a method throws an Exception, forces the programmer calling the method to deal with an Exception if one occurs

Not necessary if you are talking about exceptions in general and not the Exception class. Exception classes which extend RuntimeException and Error can be used in the throws clause without forcing the client or the consumer to catch them. This is normally done for documentation purposes but is perfectly legal and common scenario.

Not necessary if you are talking about exceptions in general and not the Exception class. Exception classes which extend RuntimeException and Error can be used in the throws clause without forcing the client or the consumer to catch them. This is normally done for documentation purposes but is perfectly legal and common scenario.

Precisely, thats why you are never forced to catch the NullPointerException even if it is thrown.

Not necessary if you are talking about exceptions in general and not the Exception class. Exception classes which extend RuntimeException and Error can be used in the throws clause without forcing the client or the consumer to catch them. This is normally done for documentation purposes but is perfectly legal and common scenario.

I'm not sure if I was talking about Exception [class] or Exceptions in general, but I realized that additional piece of information you mentioned after I posted, hence the edit with the link. Thanks for clarifying though as I should've made it more clear.

Edited 6 Years Ago by BestJewSinceJC: n/a

This article has been dead for over six months. Start a new discussion instead.