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 7 of 7

Thread: (while) insted of (try and catch)

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default (while) insted of (try and catch)

    Hi, could someone help please.

    I need to know how to use while statement instead of try and catch to execute this program please help .

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package testtrycatch;
     
    /**
     *
     * @author group4
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           int i =1;
           int j =0;
     
     
           try { System.out.println("Try block entered " +"i = " + i +" j = " +j);
     
                 System.out.println(i/j);
     
                 System.out.println("Ending try block");
           }
     
           catch (ArithmeticException e)
           {    System.out.println("Arithmetic exception caught ");
     
           }
           System.out.println("After try block ");
           return;
        }
     
    }


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: (while) insted of (try and catch)

    Link
    123456

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Exclamation Re: (while) insted of (try and catch)

    Quote Originally Posted by Bryan View Post
    Link
    123456
    Although he could easily google a while loop, the problem is with this piece of code. If he doesn't understand what is happening then googling a while loop wont help that much.

    b109 -

    I don't understand why you are looking for a while loop? The try/catch is catching the exception generated here: System.out.println(i/j);

    Without this piece of code, the exception is never generated.

    The program is executed from the main method. What are you trying to achieve with a while loop? More information is needed.

    An example of a while loop is this:

    public class WhileLoopExample {
     
    	public static void main(String[] args) {
     
    		boolean myBoolean = true;
     
    		while(myBoolean){
     
    			System.out.println("JavaProgrammingForums.com");
     
    		}
     
    	}
     
    }

    This code will keep printing JavaProgrammingForums.com to the console until the myBoolean value is changed to false.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. The Following 2 Users Say Thank You to JavaPF For This Useful Post:

    b109 (June 2nd, 2010), javanub:( (May 13th, 2010)

  5. #4
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: (while) insted of (try and catch)

    why dun wanna use try catch

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: (while) insted of (try and catch)

    well, there is the whole issue of looping. try/catch blocks execute the code inside them only once/call. Also, try/catch blocks are fairly slow (depending on the situation, can take milli-seconds where simple value checking using if/else or other conditionals are maybe an order or two of magnitude faster). If you want a pseudo replacement for try/catch, use if/else statements for the comparisons and handle the code flow as needed.

    The only time you really should be using try/catch blocks is when there is a situation that you don't want a method to handle a certain type of situations, usually from bad data passed in.

  7. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    javanub:( (May 13th, 2010), Json (May 13th, 2010)

  8. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: (while) insted of (try and catch)

    Good answer helloworld

    // Json

  9. #7
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: (while) insted of (try and catch)

    i see. thankyou for your enlightenment

Similar Threads

  1. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 02:18 PM
  2. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 03:20 PM