Java特点:
跨平台:一次编译 随处运行(因为有JVM机制);
安全:内存 (内存自动申请 内存自动释放 GC机制 垃圾回收机制 )
简洁性,简单
健壮性,异常机制;
错误 Error,源码的问题 机器配置的问题,错误是代码解决不了 要么改动源码 要么改动机器配置;
堆内存溢出错误 栈内存溢出错误
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.edu.www.day0424.Test.main(Test.java:7)
Exception in thread "main" java.lang.StackOverflowError
异常 Exception
异常
-
Exception 异常 不正常的事件
-
Error错误是所有错误类型的父类型; Exception类型是所有异常类型的父类型;
-
Error 和Exception的父类型 是 Throwable
-
Exception 的子类型
-
一类是运行时异常父类型是RuntimeException都是运行时异常;
-
一类编译时异常只要不是RuntimeException子类型;
-
-
运行时异常 可以不处理 编译也能通过 非受检异常 ;
-
编译时异常 必须处理 编译才能通过 受检异常;
1,异常的分类
/**
*2个子类型 编译器对其态度完全不同
*/
private static void method4() {
Error e;
StackOverflowError stackOverflowError;
Exception exception;
RuntimeException runtimeException;
ArithmeticException arithmeticException;
// 运行时异常 可以不处理 编译也能通过 非受检异常 ;
System.out.println(1 / 0);
System.out.println("hello");
//编译时异常
FileNotFoundException f;
//编译器 编译时异常 必须处理 编译才能通过 受检异常;
try {
FileInputStream inputStream = new FileInputStream("");
} catch (FileNotFoundException e2) {
}
}
2,常见的异常类型
/**
* 常见异常类型
*/
private static void method5() {
int[] arr = new int[3];
//数组索引越界
ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException;
System.out.println(arr[1]);
String str = "null";
//空指针异常
NullPointerException nullPointerException;
System.out.println(str.equals("y"));
//算术异常
ArithmeticException arithmeticException;
System.out.println(1 / 2);
System.out.println("hello");
Scanner sc = new Scanner(System.in);
InputMismatchException input;
int i = sc.nextInt();
}
1.1.异常的处理
1.1.1. 抓异常
-
负责任
1,try
-
try块中放 有可能出现异常的代码,try中一旦产生异常 直接跳出try块
-
有异常出现 就是有一个异常对象产生了
private static void method6() {
try {
//try块中放 有可能出现异常的代码,try中一旦产生异常 直接跳出try块
//有异常出现 就是有一个异常对象产生了
System.out.println(1 / 0);
System.out.println("14line===============");
//只有当类型匹配(也可以是父类型)的时候 才会进入catch块 一旦进入catch表示抓住了
} catch (ArithmeticException e) {
//向上转型
//Exception e2 = new ArithmeticException();
//异常的处理代码
System.out.println(e);
//获取异常信息
System.out.println(e.getMessage());
//打印堆栈报错信息 追踪记录
// e.printStackTrace();
}
System.out.println("hello");
}
2,catch
-
只有当类型匹配(也可以是父类型)的时候 才会进入catch块 一旦进入catch表示抓住了
多重catch
-
小类型在上 大类型在下面;异常对象一旦被抓捕之后 不会在继续向下匹配了;只能被抓捕一次
/**
* 多重catch 小类型在上 大类型在下面
* 异常对象一旦被抓捕之后 不会在继续向下匹配了;只能被抓捕一次
*
* @param str
*/
public static void method7(String[] str) {
int i = 0;
try {
i = Integer.parseInt(str[0]);
} catch (NullPointerException e) {
System.out.println("空指针异常产生了 抓捕到了");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组索引越界异常产生了 抓捕到了");
} catch (NumberFormatException e) {
System.out.println("格式化转换异常产生了 抓捕到了");
} catch (Exception e) {
System.out.println(" 兜底 ");
}
System.out.println(i);
System.out.println("method7 end");
}
3, finally
/**
* finally
*/
private static void method9() {
try {
System.out.println(1 / 2);
System.out.println("14line===============");
//return一旦执行 表示方法结束;
return;
} catch (NullPointerException e) {
System.out.println("异常抓捕到了");
} finally {
//最终会执行的代码 一定会执行的代码(不管有没有异常;也不管异常有没有被处理)
// 有关资源的释放 的代码 ;
System.out.println("一定会执行的代码");
}
System.out.println("hello");
}
1.1.2.抛异常
-
有异常对象 抛给上层调用者
1,运行时异常
-
运行时异常 JVM自动创建的,而且自动抛出来 如果没有抓捕 会自动抛给上层调用者(谁调用你 就是你的上层);
public class ExceptionExer2 {
public static void main(String[] args) {
// FileInputStream fileInputStream = new FileInputStream("");
}
private static void method3() {
try {
method2(10, 0);
} catch (ArithmeticException e) {
System.out.println("main 解决了");
}
System.out.println("main 执行完毕");
}
public static void method2(int op1, int op2) {
method1(op1, op2);
System.out.println("method2 执行完了");
}
/**
* 运行时异常 JVM自动创建的,而且自动抛出来 如果没有抓捕 会自动抛给上层调用者(谁调用你 就是你的上层);
*
* @param op1
* @param op2
*/
public static void method1(int op1, int op2) {
int re = op1 / op2;
System.out.println("re: " + re);
System.out.println("method1 执行完了");
}
}
2,编译时异常
public class ExceptionExer3 {
public static void main(String[] args) {
method2();
System.out.println("main end");
}
public static void method2() {
try {
method1("a.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("method2执行完毕");
}
/**
* throw 出现在方法体中 ,throw 异常对象; throw一旦执行 也表示方法的结束 下面的代码不再执行
* throws 出现方法声明处 表示此方法会抛出 的异常类型;throws后面都是编译时异常类型 如果有多个 逗号隔开
*
* @param fileName
* @throws FileNotFoundException
*/
public static void method1(String fileName) throws FileNotFoundException, IOException {
//
if (fileName.equals("a.txt")) {
FileNotFoundException fileNotFoundException = new FileNotFoundException();
RuntimeException runtimeException = new RuntimeException();
// throw 异常对象;
throw fileNotFoundException;
// System.out.println("hello");
}
if (fileName.length() <= 0) {
throw new IOException();
}
System.out.println("method1 正常结束");
}
}
1.2.自定义异常
-
只 需要定义的类型 继承 异常体系中任何一个类型
public class AgeInvalidException extends RuntimeException {
public AgeInvalidException() {
}
public AgeInvalidException(String message) {
super(message);
}
}
使用
public class ExceptionExer4 {
public static void main(String[] args) {
method2(152);
System.out.println("main end");
}
public static void method2(int age) {
if (age <= 0 || age > 150) {
throw new AgeInvalidException("年龄不合法");
}
System.out.println("method1 end");
}
/*public static void method1(int age) throws AgeInvalidException {
if (age <= 0 || age > 150) {
throw new AgeInvalidException("年龄不合法");
}
System.out.println("method1 end");
}*/
}
1.3.开发规范
-
对于运行时异常 一般做的是参数的有效性校验,提前排查出来异常
public static void main(String[] args) {
Student stu1 = null;
method3(stu1);
}
/**
* 非空校验
* @param arr
* @throws Exception
*/
public static void method4(int[] arr) throws Exception {
if (arr == null) {
throw new NullPointerException("");
}
if (arr.length == 0) {
throw new Exception("");
}
}
public static void method3(Student student) {
//校验参数
if (student == null) {
throw new NullPointerException(" ");
}
int id = student.getId();
String name = student.getName();
}
public static void method2(int op1, int op2) {
if (op2 != 0) {
int re = op1 / op2;
}
}
public static void method1(String str) {
//1 有效性校验
if (str != null) {
if (str.equals("y")) {
}
System.out.println("");
} else {
throw new NullPointerException("str 为null");
}
}
491

被折叠的 条评论
为什么被折叠?



