6

Can somebody explain me what is the use of throws Exception in java? Its just used to indicate that the method will throw the exception specified? The calling method need to catch the exception specified?

So instead of throws we can use a try-catch block to catch the exception?

How it differs from throw? Thanks

mee
  • 821
  • 5
  • 12
  • 28
  • 3
    Please read the [Java tutorial on exception](http://docs.oracle.com/javase/tutorial/essential/exceptions/). – assylias Apr 17 '12 at 08:31
  • You shouldn't catch the Exception unless you are going to do something useful with it. Just printing it and pretending it didn't happen or ignoring it is usually a bad idea. Often passing it to the caller is the best apporach. eveen for `main(String[])` you can have `throws Exception` – Peter Lawrey Apr 17 '12 at 08:32

6 Answers6

11

Java uses explicit exception handling - except of RuntimeExceptions, every exception thrown [by the method itself, or a method it invokes declares it throws it]- must be handle or declared in the method signature.

It allows safety, since when you invoke a method you know exactly which errors might occur, which you can then either handle locally with try/catch blocks, or declare as part of your method signature.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
amit
  • 175,853
  • 27
  • 231
  • 333
  • 3
    +1 "you know exactly which run-time errors might occur". Yes, but unfortunate phrasing, because RuntimeExceptions and Errors are just what does not need to get declared. (No better term off the top of my head, though). – Thilo Apr 17 '12 at 08:28
  • ok Thanks, so its a way to indicate that the calling method that such exception might occur? – mee Apr 17 '12 at 08:30
  • @Thilo: yea, I noticed it as well, and that's why I remembered to indicate the RuntimeException issue in the first sentence of the answer. I'll be glad to edit if better terminology is suggested :) – amit Apr 17 '12 at 08:30
  • 1
    @amit: FWIW, I took a swing at rewording that last sentence vis-a-vis your exchange with Thilo above. Just revert it if you don't like it, but I think it's a bit clearer. – T.J. Crowder Apr 17 '12 at 08:32
  • @T.J.Crowder: seems good to me. I always encourage editting me for better phrasing, as my info page explicitly says, since I am not a native english speaker. thanks. :) – amit Apr 17 '12 at 08:35
7

Although @amit already gave very good answer I just want to add something.

So instead of throws we can use a try-catch block to catch the exception?

I think that this part of your question was not answered. Actually you are asking whether you should define methods that are "transparent" for exceptions or catch exceptions thrown within the method.

The answer is that it depends on your application. Generally there are 2 cases when you want to catch exception into the method.

  1. you have something to do with the exception, so your flow depends on the fact that the exception was thrown. For example if you are reading from file and IO error happens you are trying to read from the file again.
  2. You do not want to expose the exceptions thrown on specific layer of your application to higher level layers. In this case you will probably wrap your code with try block and wrap thrown exception with other level exception:

    try { // some code } catch(IOException e) { throw new ApplicationLevelException(e); }

In most other cases you will probably want to be transparent for exceptions and catch all exception in one single point that knows what to do with them. For example shows customer facing error message.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

The throws keyword indicates that that method COULD throw an exception.
You should use this when you think the caller wants to do his own exception-handling, else you should use a try-catch block and return a specified error value in the catch block, like indexOf() (from an Array) returns -1 instead of throwing an exception.

11684
  • 7,356
  • 12
  • 48
  • 71
0

In short, if you are using a try-catch block for a particular exception, you are handling the exception there. If you specify throws on the method, you are declaring that the method can throw such an exception(and not handle it within itself) and it becomes the responsibility of the caller to handle that exception.

Sumit Bisht
  • 1,507
  • 1
  • 16
  • 31
0

If you add throws to a method it means this method can throw an Exception to the method that calls it.

For example:

public void method1() throws Exception {
    ...
    throw new Exception();
    ...
}

public void method2() {
    try {
        method1();
    catch(Exception e) {
        ...
    }
}

If you dont tell a method to throw the Exception you have to use try and catch to handle it.

For more information about exception handling see the documentation: http://docs.oracle.com/javase/tutorial/essential/exceptions/

tbraun89
  • 2,246
  • 3
  • 25
  • 44
0

Although, a proper answer is already given, I would like to add that whenever a try-catch block is used, a new thread is created for handling the exception. This may sometimes cause problems when activities performed in the main thread and the try-catch block thread cause any conflicts.

So it is always better to declare your API methods to throw exceptions uding the throws clause and handle the exceptions in the application which uses your API.

Using try-catch blocks is the last option!;)

TechSpellBound
  • 2,505
  • 6
  • 25
  • 36