Exceptional Handling

This code takes an integer input from the user. If the user inputs a number, the entered number shall be displayed to the user. If user input anything unexpected then the program will terminate with the error regarding invalid entrty.

package com.gl;

import java.util.Scanner;

public class exceptionHandling {
	
	public static void main(String [] args)
	{
		try
		{
			int a;
			System.out.print("Please enter a number:");
			Scanner x=new Scanner (System.in);
			a=x.nextInt();
			System.out.println("The number you entered was:"+a);
		}
		catch (Exception e)
		{
			System.out.println("This program will terminate. Invalid Number!");
		}
		
	}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: