• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use "throw" keyword

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


There is a zero divide , and a message shows and an exception caught.




There are exceptions fro open a file, and these are caught.

There is no need to use throw keyword. But if you have your own exception that is you write it yourself.Is it right?

When to use throw keyword?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you want to initiate the throwing of an exception -- it does not have to be one that you wrote.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote: if you have your own exception that is you write it yourself.Is it right?


No; As Bear said, you can throw an Exception which is already in API . example
 
Alex Parvan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you have a Servlet or a Web Service (a java application running on a server) and a client that wants to use a method on that server. If something goes wrong on the server, you might want to throw an exception, your code will look like this:

Server:

Client:

It's not such a good example, but it's the only one i can come up with I hope this puts you on the right track.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.faqs.org/docs/javap/c9/s3.html



There are times when it makes sense for a program to deliberately throw an exception. This is the case when the program discovers some sort of exceptional or error condition, but there is no reasonable way to handle the error at the point where the problem is discovered. The program can throw an exception in the hope that some other part of the program will catch and handle the exception.



http://www.ccs.neu.edu/course/com3118/EXCEPTION.html

throws

If a method is capable of causing an exception that it does not handle, it must specify this behaviour so that callers of the method can guard themselves against that exception.

type method-name(paramlist) throws exception-list

{

//…..

}

throws clause lists the types of exceptions that a method might throw.

Necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses.


Why isn't Necessary for Error or RuntimeException?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:Why isn't Necessary for Error or RuntimeException?

Because RuntimeException and any other exceptions that subclass RuntimeException are unchecked exceptions. See Lesson: Exceptions in Sun's Java Tutorials to learn more about the difference between checked and unchecked exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic