Examples of Try Catch Exceptions
Hi All,
I have written a basic program that allows users to enter three integars on the command lines and then sums the integers, then provides an average of the three integars.
Can anyone give me a good example of how to inclue a try catch exception? In other words, how could I create an exception that states, if a User enters a Char instead of an Int, then printout "You must enter an integer."
Has anyone run into this before?
Thanks,
ch103
Re: Examples of Try Catch Exceptions
There are dozens of ways to implement your requirements. I suggest posting what you have thus far, or preferably an abbreviation of it in the form of an SSCCE to give some context. Lastly, I recommend reading the following if you have not already: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Re: Examples of Try Catch Exceptions
This will give you a good basis on catching excveptions, however it was more specified on what not to do, but it did incidentally show how to change a String into an int/long/double/BigInteger
http://www.javaprogrammingforums.com...html#post47337
Re: Examples of Try Catch Exceptions
This will give you a good basis on catching excveptions, however it was more specified on what not to do, but it did incidentally show how to change a String into an int/long/double/BigInteger
http://www.javaprogrammingforums.com...html#post47337
Re: Examples of Try Catch Exceptions
try putting Throws Exception in method title lol. beats a try / catch any day due to the extra code you'd have to write.
Re: Examples of Try Catch Exceptions
Quote:
Originally Posted by
macko
try putting Throws Exception in method title lol. beats a try / catch any day due to the extra code you'd have to write.
This is not advice I would recommend as it is both poor exception handling as well as bad practice (unless you know what you are doing and have your reasons - and eliminating extra code isn't one of them). I can actually think of quite a few scenarios where this would cause very poor software performance, but I will just point you to Tjstretch's article
Re: Examples of Try Catch Exceptions
true well he did say "examples" :P so i figured may as well mention it hehe..
Re: Examples of Try Catch Exceptions
Quote:
Originally Posted by
macko
true well he did say "examples" :P so i figured may as well mention it hehe..
Bad practice 'examples' are just that - bad practice. I know you are trying to help, but planting the seed of bad practice ends up wasting the time of one if not more people.
Re: Examples of Try Catch Exceptions
True although remember, You must learn a majority of different ways to do something. No matter if they are better or worse then the rest. Throws can be good in some situations and learning throws is absolutely necessary .. If he decides to update / use somebody's source file later to create a better project from it.
If they have a Throws Exception he will wonder what it is.
So hence best to teach him now then never.
Re: Examples of Try Catch Exceptions
Quote:
Originally Posted by
macko
True although remember, You must learn a majority of different ways to do something. No matter if they are better or worse then the rest. Throws can be good in some situations and learning throws is absolutely necessary .. If he decides to update / use somebody's source file later to create a better project from it.
If they have a Throws Exception he will wonder what it is.
So hence best to teach him now then never.
You did not phrase your advice this way - in fact you phrased it to promote one technique all for not writing extra code. We have hijacked this thread enough, if you have questions or something else to say please do so through pm or conversation
Re: Examples of Try Catch Exceptions
use try catch and finally all in one program its good practice indeed !!! put try block where you assume ,program may cayuse error and then outside the try block ,put a catch block to catch error.In case ,you may like to throw your own error/exception,use keyword 'throw' to throw the exceptions
Re: Examples of Try Catch Exceptions
Thanks, but where is the example?