Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Exclamation Exception handdling

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {

    Scanner obj = new Scanner(System.in);

    System.out.println("Enter your first Number");
    int a = obj.nextInt();
    System.out.println("Enter Your Second Number");
    int b = obj.nextInt();
    int sum = a / b;
    System.out.println(sum);
    throws BadNumberException{
    if(b==0){
    throws new BadNumberException("cannot divide by zero");

    }



    }


    }
    }
    in the above code the bold text are wrong how to correct them

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    It's 'throw' not 'throws'; check any Java text book for the syntax.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    spoon-feeding )
    Java Code:
    import java.util.Scanner;
    
    class BadNumberException extends Exception {
        public BadNumberException(String message) {
            super(message);
        }
    }
    public class Main {
    
        public static void main(String[] args) throws Exception {
            Scanner obj = new Scanner(System.in);
            System.out.println("Enter your first Number");
            int a = obj.nextInt();
            System.out.println("Enter Your Second Number");
            int b = obj.nextInt();
            if(b==0) {
                throw new BadNumberException("cannot divide by zero");
            }
            int sum = a / b;
            System.out.println(sum);
        }
    }
    Skype: petrarsentev
    http://TrackStudio.com

  4. #4
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Correct code

    Hi
    First of all your syntax is wrong for throws it is as follows
    Syntax: return-type method-name(parameters-list) throws exception-list{
    body of method
    }

    Complete and Running code is as follows
    __________________________________________________ ______________


    import java.util.Scanner;

    class BadNumberException extends Exception{
    public BadNumberException(String message){
    super(message);
    }
    }

    public class Main {
    public static void main(String[] args) {
    try{
    Scanner obj = new Scanner(System.in);
    System.out.println("Enter your first Number");
    int a = obj.nextInt();
    System.out.println("Enter Your Second Number");
    int b = obj.nextInt();
    if(b==0){
    throw new BadNumberException("cannot divide by zero");
    }
    else{
    int sum = a / b;

    System.out.println("Result is : "+sum);
    }
    }catch(BadNumberException e){
    System.out.println(e.getMessage());
    }

    }

    }
    __________________________________________________ ____________

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  2. Replies: 1
    Last Post: 02-06-2011, 05:38 AM
  3. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  4. Exception
    By PhQ in forum Networking
    Replies: 11
    Last Post: 07-28-2010, 09:37 AM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •