在程序运行过程中出现的特殊情况。
异常处理的必要性:不处理的话,可能会造成程序的中断,造成不必要的损失
Throwable类(java.lang.Throwable) java语言中所有错误和异常的超类。
- 错误(Error):一般是内存溢出、虚拟机崩溃、硬件错误等问题会抛出Error,是应用程序不应该试图捕获的严重问题,是处理不了的。
- 异常(Exception):指出了合理的应用程序想要捕获的条件。是可以处理的。异常又可以分为RuntimeException(运行时异常,可以处理也可以不处理)和检查时异常(必须处理)
比较简单就不进行举例子了
- NullPointerException(空指针异常)
- ArrayIndexOutOfBoundsException(数组越界异常)
- ClassCastException(类型转换错误异常)
- NumberFormatException(数字格式化异常)
- ArithmeticException(算术异常)
- IOException
- ClassNotFoundException
- 自动抛出异常:程序运行不符合规范的代码或者结果的时候,会自动抛出异常。
- 手动抛出异常:语法为 throw new 异常类型(“实际参数”)
- 产生异常结果:相当于遇到return语句,导致程序因异常终止。
按照方法的调用链反向传递,如果始终没有处理异常。最终会传递到JVM进行异常处理(打印堆栈跟踪信息)并中断程序。
7. 异常处理- try:可能出现异常的代码
- catch:捕获异常
- finally:异常最终处理,释放资源
- throw:抛出异常
- throws:声明异常
try{ } catch{ } try{ } catch{ } catch{ }... try{ } catch{ } finally{ } try{ } catch{ } catch{ } finally{ } try{ } finally{ }
要注意的是,在抛出异常的时候,抛出异常要与catch捕获异常要匹配。
最后:try{ } finally{ } 不能捕获异常,仅仅用作释放资源。一般用在底层代码。
public static void divide(){ Scanner input = new Scanner(System.in); System.out.println("请输入两个数字:"); try { int num1 = input.nextInt(); int num2 = input.nextInt(); System.out.println(num1/num2); } finally { System.out.println("释放资源"); } } public static void main(String[] args) { try{ divide(); } catch (Exception e){ System.out.println("上层任务处理异常"); } }
运行结果
举例子
//声明异常:通知调用者,我存在异常,向上抛出自己的问题 //声明检查时异常:调用者必须处理或者向上继续抛出 //声明运行时异常,调用者可以处理也可以不处理 public static void divide() throws Exception{ Scanner input = new Scanner(System.in); System.out.println("请输入两个数字:"); int num1 = input.nextInt(); int num2 = input.nextInt(); System.out.println(num1/num2); } public static void main(String[] args) { try{ divide(); } catch (Exception e){ System.out.println("上层任务处理异常"); } }7.3 throw(抛出异常)
语法:throw + 异常对象
举例如下:
public class Student { private int ID; private String name; private int age; public Student(){ } public Student(int ID, String name, int age) throws Exception{ super(); this.setAge(age); this.ID = ID; this.name = name; } 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 Exception{ if(age >= 0 && age <=120) { this.age = age; } else { //抛出异常 throw new Exception("年龄错误"); } } @Override public String toString() { return "Student{" + "ID=" + ID + ", name='" + name + ''' + ", age=" + age + '}'; } }
测试类如下:
public class TestStudent { public static void main(String[] args) { try { Student student = new Student(1,"xiaoli",300); System.out.println(student.toString()); } catch (Exception e){ System.out.println(e.getMessage()); } } }8. 自定义异常
因为系统提供的异常有时候是表达不清楚异常的类型的。需要定义自己的异常类,来更准确的来表达含义。
public class AgeException extends RunTimeException { public AgeException() { super(); } public AgeException(String message) { super(message); } public AgeException(String message, Throwable cause) { super(message, cause); } public AgeException(Throwable cause) { super(cause); } public AgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
public static void main(String[] args) { Student student = new Student(1,"xiaoli",300); System.out.println(student.toString()); }
运行结果
注:此处的是运行时异常,上层任务可以不对该异常进行处理,从而JVM最后将其打印出来了。
- try catch finally里面是否可以写return,如果写了可以 那么try里面的return执行了之后是否还会执行finally。
回答:都可以写return,但是不建议在finally里面添加,因为finally会执行在try的return之前,如果finally里面有return的话,就会直接在finally里面返回,导致返回的结果不是所需要的那个。
但是有例外如果try里面包含System.exit(0)则不会执行finally语句。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)