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

Dani AI

Generated

Short, practical clarity for "throw" vs "throws" and what to do with exceptions.

A method uses throws in its signature to declare what checked exceptions callers must handle (the compiler uses this). Inside the method, use throw to actually raise an exception instance. Example:

public void readFile(String path) throws IOException {
    if (path == null) throw new IllegalArgumentException("path is null");
    // file I/O that may throw IOException
    throw new IOException("read failed");
}

Create checked and unchecked custom exceptions depending on intent. Checked exceptions (extend Exception) signal recoverable conditions the caller should deal with. Unchecked exceptions (extend RuntimeException) represent programming errors or conditions you do not expect callers to be forced to catch. Example skeletons:

class MyChecked extends Exception {
    public MyChecked(String msg, Throwable cause) { super(msg, cause); }
}

class MyUnchecked extends RuntimeException {
    public MyUnchecked(String msg) { super(msg); }
}

Practical rules-of-thumb: declare specific exception types rather than throws Exception; avoid catching and swallowing exceptions silently; preserve the original cause when rethrowing (use a constructor that accepts cause) so stack traces remain useful; prefer try-with-resources for I/O. Example rethrow pattern:

try (InputStream in = ...) {
   // ...
} catch (IOException e) {
   throw new DataAccessException("read failed", e);
}

Notes tied to this thread: was right that the thrown type must be in the Throwable hierarchy; and added the important distinction about unchecked exceptions not forcing caller handling; this should clarify the syntax and when to use each approach. For official background and more examples, see Oracle's Java tutorial on exceptions and the Throwable API documentation (search "Java exceptions tutorial" or "java.lang.Throwable" on Oracle docs).

Recommended Answers

All 4 Replies

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.