为什么以下代码不能捕获异常 [英] Why the following code doesn't catch the exception

查看:92
本文介绍了为什么以下代码不能捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下代码在用户输入字母时未捕获异常。相反,如果用户输入的数字不是给定的选择,则会触发异常。



这不是完整的代码。



我尝试过:



 while(true){
系统。 out.println(吸引力的\\\
List:);
System.out.println(\ nSport:[1] Ferringhi Sea Sports Co.);
System.out.println(文化:[2]槟榔土生华人博物馆);
System.out.println(商店:[3] Queensbay Mall);
System.out.println(食物:[4] Restoran Nasi Kandar Line Clear);
System.out.println([5]退出);
System.out.print(\ n输入吸引力的更多细节:);
int typeID = input.nextInt();

// Sport s = new Sport(1,ESCAPE Theme Park,139.58,828 Jalan Teluk Bahang,Teluk Bahang,Penang Island 11050,Malaysia,4.5);
尝试{
if(typeID == 1)
{
a [0] = new Sport(typeID,Ferringhi Sea Sports Co.,9 AM-7:30PM ,Wave Runner Watersport,Foot Reflexology& Fish Spa,Batu Ferringhi,George Town,Penang Island,Malaysia,4.7);
System.out.println(a [0]);
}
else if(typeID == 2)
{
a [1] = new Culture(typeID,Pinang Peranakan Museum, - Adult(RM20.00)\\ -Child 6以下(免费),29,Church St,George Town,10200 George Town,Pulau Pinang,2楼,周一至周日,包括公众假期,上午9:30至下午5点 ,4.4);
System.out.println(a [1]);
}
否则if(typeID == 3)
{
a [2] =新店铺(typeID,Queensbay Mall,该商场有5个零售级别的国际和本土品牌包括时尚,生活方式,餐饮,娱乐,体育,电子,家居和百货商店。,1F-78 Queensbay Mall,100 Persiaran Bayan Indah Bayan Lepas,11900 George Town,Penang,10:30 AM -10:30PM,4.4);
System.out.println(a [2]);
}
else if(typeID == 4)
{
a [3] = new Food(typeID,Restoran Nasi Kandar Line Clear,6.50 for a rice of rice ,炸鸡,鸡蛋,蔬菜和混合的肉汁,在161和177槟城路,乔治城,10000乔治城,Pulau Pinang,24小时,3.6);
System.out.println(a [3]);
}
else if(typeID == 5)
break;
else
抛出新的InputMismatchException();
}
catch(InputMismatchException ex)
{
System.out.println(输入错误:错误的输入!);
input.nextLine();
}
} //结束while1循环for city1

解决方案

你应该抓住<抛出的异常code> Scanner.nextInt 方法。


您的尝试位置错误。它应该是:

  while (true){
System.out.println(< span class =code-string> \ n吸引力列表:);
System.out.println( \ nSport:[1] Ferringhi Sea Sports Co。);
System.out.println( 文化:[2] Pinang Peranakan Museum);
System.out.println( 商店:[3] Queensbay Mall);
System.out.println( 食物:[4] Restoran Nasi Kandar Line Clear );
System.out.println( [5]退出);
System.out.print( \ n输入引用号码以获取更多详情:);
尝试 {
int typeID = input.nextInt();


Why the following code doesn't catch the exception when user entered letter. Instead, the exception caught if user entered a number other than the choice given.

This is not a full code.

What I have tried:

while(true) {
			System.out.println("\nList of Attraction: ");
			System.out.println("\nSport: [1]Ferringhi Sea Sports Co.");
			System.out.println("Culture: [2]Pinang Peranakan Museum");
			System.out.println("Shops: [3]Queensbay Mall");
			System.out.println("Food: [4]Restoran Nasi Kandar Line Clear");	
			System.out.println("[5] Exit");			
			System.out.print("\nEnter no. of attraction for more details: ");
			int typeID = input.nextInt();
			
			//Sport s = new Sport(1, "ESCAPE Theme Park", 139.58, "828 Jalan Teluk Bahang, Teluk Bahang, Penang Island 11050, Malaysia", 4.5);
			try {
			if(typeID==1)
			{
				a[0] = new Sport(typeID, "Ferringhi Sea Sports Co.", "9AM-7:30PM", "Wave Runner Watersport, Foot Reflexology & Fish Spa", "Batu Ferringhi, George Town, Penang Island, Malaysia", 4.7);
				System.out.println(a[0]);
			}
			else if(typeID==2)
			{
				a[1] = new Culture(typeID, "Pinang Peranakan Museum", "-Adult (RM20.00)\n\t   -Child below 6 (Free)", "29, Church St, George Town, 10200 George Town, Pulau Pinang, Floor 2", "Monday to Sunday including Public Holidays from 9:30 am to 5 pm", 4.4);
				System.out.println(a[1]);
			}
			else if(typeID==3)
			{
				a[2] = new Shops(typeID, "Queensbay Mall", "The mall features 5 retail levels of international and home-grown brands including fashion, lifestyle, dining, entertainment, sports, electronics, home furnishing and department store.", "1F-78 Queensbay Mall, 100 Persiaran Bayan Indah Bayan Lepas, 11900 George Town, Penang", "10:30AM-10:30PM", 4.4);
				System.out.println(a[2]);
			}
			else if(typeID==4)
			{
				a[3] = new Food(typeID, "Restoran Nasi Kandar Line Clear", "6.50 for a plate of rice, fried chicken, egg, vegetables and a mix of gravies", "Beside 161 & 177 Penang Road, George Town, 10000 George Town, Pulau Pinang", "24 hours", 3.6);
				System.out.println(a[3]);
			}
			else if(typeID==5)
				break;
			else
				throw new InputMismatchException();
			}
			catch(InputMismatchException ex)
			{
				System.out.println("Input Error: Wrong Input!");
				input.nextLine();
			}
			} // end while loop for city1

解决方案

You should catch the exception thrown by the Scanner.nextInt method.


Your try is in the wrong place. It should be:

while(true) {
			System.out.println("\nList of Attraction: ");
			System.out.println("\nSport: [1]Ferringhi Sea Sports Co.");
			System.out.println("Culture: [2]Pinang Peranakan Museum");
			System.out.println("Shops: [3]Queensbay Mall");
			System.out.println("Food: [4]Restoran Nasi Kandar Line Clear");	
			System.out.println("[5] Exit");			
			System.out.print("\nEnter no. of attraction for more details: ");
    try {
			int typeID = input.nextInt();


这篇关于为什么以下代码不能捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆