欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java,

来源: javaer 分享于  点击 11680 次 点评:161

java,


package Exception;

import java.util.Scanner;

public class TextNumber {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("请输入课程代号(1~3之间的数字):");
		int lve=scanner.nextInt();
		switch(lve){
		   case 1:
			   System.out.println("C#编程");
			   break;
		   case 2:
			   System.out.println("Java编程");
			   break; 
		   case 3:
			   System.out.println("SQL基础");
			   break;
			default:
				 System.out.println("你输入的不是1~3之间的数字!");
				 break;
		}
		System.out.println("欢迎提出建议!");

	}

}

2.
package Exception;

public class Person {
	private String name="";
	private int age=0;
	private String sex="男";
	public void setAge(int age) throws Exception{
		if(age>0&&age<100)
			this.sex=sex;
		else{
			throw new Exception("年龄必须在1~100之间!");
		}
	}
	public void print(){
		System.out.println(this.name+"("+this.sex+","+this.age+"岁)");
	}
	
}
package Exception;

public class TextException2 {

	public static void main(String[] args) {
		Person person=new Person();
		try{
			person.setAge(199);
			person.print();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

}


3.简述java异常体系结构
基本类型异常:Exception 异常层次结构的父类
ArithmeticException 算术错误情形,如以零作除数
ArrayIndexOutOfBoundsException 数组下标越界
NullPointerException 尝试访问 null 对象成员
ClassNotFoundException 不能加载所需的类
IllegalArgumentException 方法接收到非法参数
ClassCastException 对象强制类型转换出错
NumberFormatException 数字格式转换异常,如把"abc"转换成数字
最高级:object 其次:Throwable 再分为Exception 和Error,其中Exception 包括基本类型异常

相关文章

    暂无相关文章
相关栏目:

用户点评