Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Examples of Try Catch Exceptions

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default 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.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Examples of Try Catch Exceptions

    Quote Originally Posted by macko View Post
    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

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Examples of Try Catch Exceptions

    true well he did say "examples" so i figured may as well mention it hehe..

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Examples of Try Catch Exceptions

    Quote Originally Posted by macko View Post
    true well he did say "examples" 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.

  9. #9
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default 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.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Examples of Try Catch Exceptions

    Quote Originally Posted by macko View Post
    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

  11. #11
    Junior Member Rex the king's Avatar
    Join Date
    Oct 2011
    Location
    INDIA
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  12. #12
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Examples of Try Catch Exceptions

    Thanks, but where is the example?

Similar Threads

  1. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 11:14 AM
  2. Always Catch Exceptions
    By Tjstretch in forum Java Programming Tutorials
    Replies: 2
    Last Post: October 27th, 2011, 02:26 PM
  3. Program layout examples please
    By derekxec in forum Java Theory & Questions
    Replies: 1
    Last Post: July 2nd, 2011, 11:05 AM
  4. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 03:20 PM