1,Exception 异常 java中程序一旦遇到异常,程序无法继续向下执行
Throwable
/
Error Exception
2,Error: 错误,一般是由虚拟机生生成并脱出的,不需要程序猿控制,程序媛不关注
Exception : 异常
检查时异常|编译时异常 : 编译期间遇到的异常,如果不处理,程序语法运行
运行时异常 : 运行期间遇到的异常,通过增强程序健壮性的代码就可以解决 if...
3,常见的运行时异常:
1.空指针异常 NullPointerException
2.数组索引越界 ArrayIndexOutOfBoundsException
3.类型转换异常 ClassCastException
4.数学异常 ArithmeticException
5.格式转换异常 NumberFormatException
public class Class01_Exception {
public static void main(String[] args) {
//1.空指针异常 NullPointerException
System.out.println("main----->开始了");
String str = null;
if (str!=null)
System.out.println(str.length());
System.out.println("main----->结束了");
//2.数组索引越界 ArrayIndexOutOfBoundsException
int[] arr = new int[5];
int i = 6;
if (i>=0 && i
4.3.2异常处理方案*
1,异常处理方案: * 1.抛出异常 throws 异常类型 2.异常捕获 try...catch try{ 有可能出现异常的代码; }catch(FileNotFoundException e){ 遇到异常后执行的代码; }catch(NullPointerException e){ 遇到异常执行的代码; }..... catch(Exception e){//-->接盘侠 //Exception e = new ClassCastException(); } finally{ 无论try中是否遇到异常,结束之前都会执行finally中的代码 }
}
2,
注意: 无论是编译时异常还是运行时异常都可以通过2中解决方案处理 但是编译时异常只能通过2种解决方案处理 而运行时异常可以通过2种解决方案处理,也可以通过增强程序健壮性if 一个try后面可以接1~n个catch 如果try中的代码一旦遇到异常try中后面的代码不会执行,会直接判断catch,多个catch之间从上到下判断,如果找到对应的catch,执行{}中的代码 如果存在多个catch,大范围类型的捕获放在最后 一般在finally中会定义资源的关闭等代码
public class Class02_Exception {
public static void main(String[] args) {
System.out.println("main----------");
try {
System.out.println("try------>开始了");
//运行时异常
test();
System.out.println(10/0);
System.out.println("try------>结束了");
} catch (FileNotFoundException e) {
System.out.println("遇到文件未找到异常");//
}catch (ArithmeticException e){
System.out.println("分母为0......");
}finally {
System.out.println("世上没有不散的宴席");
}
System.out.println("------结束了------");
}
public static void test() throws FileNotFoundException {
//编译时异常
InputStream in = new FileInputStream("D://test.txt");
}
}
4.3.3自定义异常
1,自定义异常 自定义的异常类型需要直接或者间接的继承自Exception 自定义的异常类型为运行时异常需要直接或者间接的继承自RuntimeException throw 制造异常
public class Class03_Exception {
public static void main(String[] args) {
//创建对象
User num = new User();
num.setId(123456);
num.setName("戴总");
//num.setAge(20);
//AgeException作为运行时异常
//AgeException作为编译时异常
try {
num.setAge(28);
} catch (AgeException e) {
try {
num.setAge(18);
} catch (AgeException ageException) {
ageException.printStackTrace();
}
}
System.out.println(num);
}
}
//自定义异常
//class AgeException extends RuntimeException { //运行时异常
class AgeException extends Exception{//编译时异常
public AgeException(){
}
public AgeException(String message){
super(message);
}
}
class User{
private int id;
private String name;
private int age;
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//运行时异常
//编译时异常
public void setAge(int age)throws AgeException {
if (age<18 || age>100){
throw new AgeException(age+"不合法");
}
this.age=age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
'}';
}
}



