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

Thread: How to catch exceptions in Java

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How to catch exceptions in Java

    Hi
    My name is Raj and joined this forum recently. I have just started to learn Java Programming. I was interested in adding error handling mechanism to my program whose code is written below. The programs runs fine but if someone enter a character instead of integer program end with some error message. Kindly someone put the code of error mechanism in my program code as an illustration-
    import java.util.Scanner;
     
    public class Calci
    { 
     
     public static void main(String args[])
     {
      for(;;)
      {
       int choice;
       Scanner in = new Scanner(System.in);
       System.out.println("*****************MENU***********************");
       System.out.println("1.Add Numbers " + "      2.Substract Numbers");
       System.out.println("3.Multiply numbers " + " 4.Divide Numbers");
       System.out.println("*************Press '5' to Exit**************");
       System.out.println(" Enter your menu Choice");
       choice=in.nextInt();
     
     
       switch(choice)
       {
        case 1:
        int addr,addl,sum=0;
        System.out.println("How many nos you want to add");
        addr=in.nextInt();
        int a[]=new int[addr];
        System.out.println("Enter " + addr + " numbers");
        for(addl=0;addl<addr;addl++)
        {
         a[addl]=in.nextInt();
         sum = sum + a[addl];
         System.out.println("The sum of Numbers is: " + sum);
        }
        break;
     
        case 2:
        int sub2,sub1,subl;
        System.out.println("Enter the 1st number");
        sub1=in.nextInt();
        System.out.println("How many numbers you want to substract from 1st number");
        sub2=in.nextInt();
        int s[]=new int[sub2];
        System.out.println("Enter " + sub2 + " numbers");
        for(subl=0;subl<sub2;subl++)
        {
         s[subl]=in.nextInt();
         sub1 = sub1 - s[subl];
         System.out.println("The result of subtraction is: " + sub1);
        }
        break; 
     
        case 3:
        int mloop, mul,mresult=1;
        System.out.println("How many nos you want to multiply");
        mul=in.nextInt();
        int m[]=new int[mul];
        System.out.println("Enter " + mul + " numbers");
        for(mloop=0;mloop<mul;mloop++)
        {
         m[mloop]=in.nextInt();
         mresult=mresult*m[mloop];
         System.out.println("The result of multiplication is: " + mresult);
        }
        break;
     
        case 4:
        int div1,div2,divloop;
        System.out.println("Enter the dividend");
        div1=in.nextInt();
        System.out.println("How many times you want to divide the divisor");
        div2=in.nextInt();
        int d[]=new int[div2];
        System.out.println("Enter " + div2 + " divisors");
        for(divloop=0;divloop<div2;divloop++)
        {
         d[divloop]=in.nextInt();
         div1=div1/d[divloop];
         System.out.println("The result of division is: " + div1);
        }
        break;
     
        default:
        System.out.println("Wrong Choice");
        System.exit(0);
        }
       }
      }
    }
    Last edited by brajraj; September 5th, 2012 at 05:48 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to catch exceptions in Java

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Take a look at the java tutorial:
    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    brajraj (September 5th, 2012)

Similar Threads

  1. Examples of Try Catch Exceptions
    By ch103 in forum Exceptions
    Replies: 11
    Last Post: February 8th, 2012, 09:22 PM
  2. Always Catch Exceptions
    By Tjstretch in forum Java Programming Tutorials
    Replies: 2
    Last Post: October 27th, 2011, 02:26 PM
  3. can exceptions be propagated from server to client using java rmi?
    By slashdevslashnull in forum Java Networking
    Replies: 0
    Last Post: June 29th, 2011, 10:37 PM
  4. Java Exceptions
    By Vinceisg0d in forum Java Theory & Questions
    Replies: 2
    Last Post: March 13th, 2010, 12:25 AM

Tags for this Thread