Sunday, August 6, 2017

Try and Exception Handling Demo in Java

A very simple program that I wrote as requested to me by one of our visitor of my website to show how to use exception handling and do you want to continue in Java. What does the program do is to ask the user to give a number if the number is not an integer will display an error message on the screen and it will ask the user if the user wants to continue to run the program again. I hope you will find my work useful. Thank you.

I hope you will find my work useful and beneficial. If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.





Sample Program Output


Program Listing


/* Try and Exception Handling Demo in Java */
/* Author: Mr. Jake R. Pomperada, MAED-IT */
/* August 6, 2017   Sunday      2:04 PM   */
/* Mandaluyong City, Metro Manila         */
/* Philippines                            */
/* Editor : TextPAD                       */
/* Language : Java SE                     */



import java.util.*;

class test{

public static void main(String args[]){

    Scanner input= new Scanner(System.in);
    int a=0;

    char ch;

  do {

        try{
            System.out.println("\n\n");
            System.out.println("Try and Exception Handling Demo in Java");
            System.out.println("\n");
            System.out.println("Written By: Mr. Jake R. Pomperada");
            System.out.println("\n");
System.out.print("Enter a Number  : ");

            a = input.nextInt();

          System.out.println("\n");
          System.out.println("The number you just given is " + a + ".");
          System.out.println("\n");
        }catch(InputMismatchException exception){
          System.out.println("\n");
          System.out.println("Invalid Integer value. Try Again Give an Integer value");
          input.nextLine();
 }

     System.out.println("\n");
     System.out.print("\nDo you want to continue (Type y or n) : ");
     ch = input.next().charAt(0);


     } while (ch == 'Y'|| ch == 'y');

     System.out.println();
     System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
     System.out.println("\n\n");
  }

}





No comments:

Post a Comment