• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the use of throw keyword  RSS feed

 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have confused about throw keywords,it is user defiend exception,but when we use them and why we use them
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:i have confused about throw keywords,it is user defiend exception,but when we use them and why we use them

Well first, it has nothing to do with "user defined" exceptions; it has to do with any Exception that is checked (ie, it doesn't extend RuntimeException) - eg, IOException, SQLException.

Checked Exceptions must be thrown or caught, so if you don't want to write a try...catch block for a piece of code that can throw a checked exception, you must throw it.

throws clauses can also be used to document the fact that a method throws a particular Exception, even if it isn't checked, but that's more a matter of style.

HIH

Winston
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:throws clauses can also be used to document the fact that a method throws a particular Exception, even if it isn't checked, but that's more a matter of style.

I agree. It's a matter of style. IMO unchecked exception should be marked in javdoc, not with throws clause.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure? OP's question says throw not throws.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Winston Gutkowski wrote:throws clauses can also be used to document the fact that a method throws a particular Exception, even if it isn't checked, but that's more a matter of style.

I agree. It's a matter of style. IMO unchecked exception should be marked in javdoc, not with throws clause.

so throw will use instead of try catch ,am i right?when we will use throws ofcourse it is system defined excption,which scenario will use
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote: . . .
so throw will use instead of try catch ,am i right?when we will use throws ofcourse it is system defined excption,which scenario will use
No. No.

You use throw to signal that something has gone wrong and your method should not continue. You use throws to signal that your method might suffer a particular exception. Google for Java Tutorials Exceptions and read that.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
sekhar kiran wrote: . . .
so throw will use instead of try catch ,am i right?when we will use throws ofcourse it is system defined excption,which scenario will use
No. No.

You use throw to signal that something has gone wrong and your method should not continue. You use throws to signal that your method might suffer a particular exception. Google for Java Tutorials Exceptions and read that.

then we can use try and catch instead to throw?then what the purpose of throw ,is it more advantage?
below program i got an error ie


i got error ie
C:\Documents and Settings\admin\Desktop>javac Client.java
.\MyOwnExceptionClass.java:12: error: illegal character: \160
 return "Price should not be in negative, you are entered" +price;
^

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Illegal character errors usually suggest you have been using a word processor (=WP) rather than a text editor, and the WP has inserted weird control characters. I am a bit surprised about \160, however. If that is octal, it is equivalent to \u0070 which is p and if it is hex it is Š. Delete everything between the start of return and the preceding correct character and replace it all with real whitespace.

I wouldn't say either those bits of code is good, I am afraid. In the first you shouldn't throw an Exception from main if you want the application to keep running. Also don't declare Exception when you have a more specific type of Exception. That sort of code would be better if you had it in the constructor or a setPrice method, however.
I don't like the design of the Exception class. You usually simply override the constructors of the Exception class and callThere is a ready‑made Exception for that sort of situation: IllegalArgumentException, but that isn't checked. You doubtless by now know that checked exceptions provide more robustness because they must be handled. But there is a lot of controversy about that: Google for checked Exceptions: an experiment which failed, and that should give you a different opinion about that question.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But there is a lot of controversy about that: Google for checked Exceptions: an experiment which failed...

I'm not sure that it has "failed"; I'd say that, like a few other things, "it has never seriously been tried".

IOException and SQLException are common examples used to beat up on checked Exceptions; but I'd say that their very existence (and the fact that checked Exceptions are a "default") proves that the early designers didn't actually understand the nature of a checked exception.

Firstly, it's not (or shouldn't be) a default; it should be a choice. And furthermore, an informed one. The mere fact that, to this day, there is no isChecked() method says that the founding fathers had a good idea, but didn't know how to implement it.

Personally, my choice would have been an Exception.Checked marker interface that precludes (or dissuades) vast hierarchies of "checked" Exceptions but, of course, I suspect it's far too late now (? maybe not).

I've dealt with a couple of apps written by people that did understand the idea; and they were an absolute joy - simple, and self-documenting - but unfortunately, IOE and SQLE remain as "piñatas".

Winston
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Campbell Ritchie wrote:But there is a lot of controversy about that: Google for checked Exceptions: an experiment which failed...

I'm not sure that it has "failed"; I'd say that, like a few other things, "it has never seriously been tried".

IOException and SQLException are common examples used to beat up on checked Exceptions; but I'd say that their very existence (and the fact that checked Exceptions are a "default") proves that the early designers didn't actually understand the nature of a checked exception.

Firstly, it's not (or shouldn't be) a default; it should be a choice. And furthermore, an informed one. The mere fact that, to this day, there is no isChecked() method says that the founding fathers had a good idea, but didn't know how to implement it.

Personally, my choice would have been an Exception.Checked marker interface that precludes (or dissuades) vast hierarchies of "checked" Exceptions but, of course, I suspect it's far too late now (? maybe not).

I've dealt with a couple of apps written by people that did understand the idea; and they were an absolute joy - simple, and self-documenting - but unfortunately, IOE and SQLE remain as "piñatas".

Winston


What I really like about Java is the way that it is self documenting. Even if there is no Javadoc available, or it is badly written, the IDE/compiler will tell me what Exceptions I need to handle. I've used other languages that don't force you to catch Exceptions, and then have to wait until runtime to find out that an undocumented Exception was thrown due to me misunderstanding the API or not anticipating a possible failure.

I'd be interested to know what you view as the ideal way to use checked Exceptions though.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:I'd be interested to know what you view as the ideal way to use checked Exceptions though.

Simply put: individually. Yes, it means a bit more code; but its documentary code.

I don't even have any basic problem with hierarchies of checked exceptions, but they need to be documented clearly, and you need to know when it's applicable to use a superclass. Do you know all the subclasses of IOE or SQLE? I don't, and there are LOTS.

Furthermore, I don't think that either is actually a checked exception. FileNotFoundException (a subclass of IOE): absolutely; but not IOE itself.

But the basic problem is that Exceptions are checked by default, which implies a structure to the way they're handled. And that, to me, is wrong. There might (and I'm not convinced of this) be a case for saying that an unchecked exception can be "promoted" to a checked one, but the class hierarchy basically precludes it.

Of course, you can always re-throw, but personally I don't like try...catch blocks very much, and I'll usually try to avoid them if I can (generally by putting a throws clause in my method, which is documentary).

And if the above isn't sufficient, my philosophy is that methods should do something. They should not worry about exception logic. If an exception occurs as a result, it should be thrown; either directly (my preference) by the method, or given to a "handler" that is defined and documented by the app.

And that (unfortunately) means that Java programmers needs to know about exceptions like NullPointerException and ArithmeticException that can be thrown as "effects" (ie, by the JVM), not causes. In fact, I'd say that NPE (at least the one thrown by the JVM) should always have been NullPointerError.

But that's me; and I started Java only around my 20th year in the biz; so I'm quite sure there are other opinions on the subject.

Winston
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I referred to the failed links to highlight the controversy. Not to signify that I agree with them.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the output of above program,how to get it,i got error
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:i got error

What is the error?

Error messages contain a lot of useful information about what is wrong. Don't just think "Oh, an error. Now I don't know what to do." At least read and try to understand what the error message means. If you don't know, and you post a question in the forums, then include the error message, because it helps other people to understand what's wrong. If you don't tell us what the error is, you're making it hard for us to help you.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
sekhar kiran wrote:i got error

What is the error?

Error messages contain a lot of useful information about what is wrong. Don't just think "Oh, an error. Now I don't know what to do." At least read and try to understand what the error message means. If you don't know, and you post a question in the forums, then include the error message, because it helps other people to understand what's wrong. If you don't tell us what the error is, you're making it hard for us to help you.

when i compile i got this ie C:\Documents and Settings\admin\Desktop>javac Client.java
.\MyOwnExceptionClass.java:12: error: illegal character: \160
return "Price should not be in negative, you are entered" +price;
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!