异常

异常处理

Java的异常处理是通过5个关键字来实现的:【try】【catch】【finally【throw】【throws】




使用try/catch块捕获异常,分为三种情况:




示例:

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



示例:

public class Exception {
public static void main(String[] args) {
System.out.print("请输入课程代号(1至3之间的数字):");
Scanner in = new Scanner(System.in);
try {
int courseCode = in.nextInt();
switch (courseCode) {
	case 1:
		System.out.println("C#编程");
		break;
	case 2:
		System.out.println("Java编程");
		break;
	case 3:
		System.out.println("SQL基础");
}
} catch (Exception ex) {
        System.out.println("输入不为数字!");
        ex.printStackTrace();
}
System.out.println("欢迎提出建议!");
}
}



示例:

public class Exception2 {
public static void main(String[] args) {
System.out.print("请输入课程代号(1至3之间的数字):");
Scanner in = new Scanner(System.in);
try {
int courseCode = in.nextInt();
switch (courseCode) {
	case 1:
	        System.out.println("C#编程");
	        break;
	case 2:
	        System.out.println("Java编程");
	        break;
	case 3:
	        System.out.println("SQL基础");
}
} catch (NullPointerException ex) {
        System.out.println("输入不为数字!");
}
System.out.println("欢迎提出建议!");
}
}

在try/catch块后加入finally块,可以确保无论是否发生异常,finally块中的代码总能被执行




示例:

public class Exception3 {
public static void main(String[] args) {
System.out.print("请输入课程代号(1至3之间的数字):");
Scanner in = new Scanner(System.in);
try {
int courseCode = in.nextInt();
switch (courseCode) {
	case 1:
	        System.out.println("C#编程");
	        break;
	case 2:
	        System.out.println("Java编程");
	        break;
	case 3:
	        System.out.println("SQL基础");
}
} catch (Exception ex) {
        System.out.println("输入不为数字!");
} finally {
        System.out.println("欢迎提出建议!");
}
}
}

多重catch块

一段代码可能会引发多种类型的异常

当引发异常时,会按顺序来查看每个catch语句,并执行第一个与异常类型匹配的catch语句

执行其中一条catch语句之后,其后的catch语句将被忽略



示例:

public class Exception4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
System.out.print(“请输入S1的总学时:");
int totalTime = in.nextInt();            //总学时
System.out.print(“请输入S1的课程数目:");
int totalCourse = in.nextInt();       //课程数目
System.out.println(“S1各课程的平均学时为:" 
                                  + totalTime / totalCourse);
} catch (InputMismatchException e1) {
System.out.println("输入不为数字!");
} catch (ArithmeticException e2) {
System.out.println("课程数目不能为零!");
} catch (Exception e) {
System.out.println("发生错误:"+e.getMessage());
}
}
}

抛出异常

public class Teacher {
    private String id;     // 教员编号,长度应为7
    public void setId(String pId){
 // 判断教员编号的长度是否为7
 if (pId.length() == 7) {
id = pId;
 } else {
throw new IllegalArgumentException("参数长度应为7!");
 }
    }
}

public class TeacherTest {
    public static void main(String[] args) {
Teacher teacher = new Teacher();
 try {
teacher.setId("088");
 } catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
 }
     }
}

声明异常




log4j记录日志信息




日志

用来记录一些重要的才做信息

有价值的日志数据能够帮助用户提前发现和避开灾难,并且找到事件发生的原因




如何使用log4j记录日志












finally与reurn


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页