实践目的
1.了解异常的概念
2.掌握异常的抛出和捕获
3.了解自定义异常类
实践内容
1.抛出与捕获异常
public class Chap2_4_2
{
public static void main(String[] args)
{
Student s1 = new Student();
try{
System.out.println(s1.age/0);
}
catch(Exception e)//捕获异常
{
System.out.println(e.toString());//输出异常
}
System.out.println("Hello World!");
}
}
class Student
{
public String name;
public int age;
//public void reset() throw Exception
//{
// age = 1/0;
// name = "";
//}
}
总结
1.常见的异常类
NullPointerException
ArithmeticException
IndexOutOfBoundsException
2.捕获异常的程序结构
try{
}catch (){
}
finally{
}