-1

I am learning Java and come across the throws keyword. I came to know that it is used for exceptions we cant handle or don't wont to handle.

Why can't we just use exception.printstacktrace? Or is there any situation where throws can perform auto exception handling or it is just a mere keyword for the reader to know that the method can throw that exception?

Pavan Kumar
  • 77
  • 1
  • 6
  • Did you read the [Java tutorial on Exceptions](https://docs.oracle.com/javase/tutorial/essential/exceptions/)? –  May 28 '15 at 06:39
  • Checked exceptions and `throws` is used for exceptions that you generally **should** handle higher up the chain, or to document exceptions that the user of your method should be prepared to handle. – Mark Rotteveel May 28 '15 at 06:39

5 Answers5

3

If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.

You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the different in throws and throw keywords.
And'throws' does not perform any exception handling it just tells to delegate exception handling to caller method as its not done in called method.

exception.printStackTrace() 

or

exception.getMessage()

You can print stack trace only when exception catched. Basically java follows a rule Throw or handle with checked exceptions. Handling exception is catching them. Also remember that some where you always have to handle your exception but you have freedom to choose where in your application flow you want to handle.
And'throws' does not perform any exception handling it just tells to delegate exception handling to caller method as its not done in called method. So that called method is aware of exception it has to handle or throw back again(but as said earlier some where it has to be handled)

PS: there is very nice question for is printstacktrace considered bad

Community
  • 1
  • 1
Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
1

When you declare that your method throws SomeCheckedException, you require of the users of your method to handle that exception (or declare that they throw it themselves if they can't handle it). It's part of the contract of the method.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

practical usage

If you are working on enterprise applications, and any exception came in middle layers those exceptions need to propagated to controllers this is one of the practical example of throws keyword.

In such applications we write navigation logic in controller and also handle logging, so it's necessary to propagate your exception message to controller layers.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
1

dont wanna handle? never happen, you always have to handle your exceptions. But when you throw an exception, it means that you dont wanna handle this exception in this function, and you want to deal with this in another place.

Lrrr
  • 4,755
  • 5
  • 41
  • 63
1

Practical Usage depends on person's requirement !

Suppose I developed an API having a method animateImages(InputStream image) Now this method will work fine if a valid image is passed but what if you pass a PDF to this method , this method fails miserably .

So if i will handle the exception , I just put a stackTrace() and log what is wrong but that is of no use for you .

I want you to know the reason and to react accordingly and send the valid Image file or whatsoever you want to do .

This is why we generally throw any Exception when we want the consumer to handle it .

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62