@toc
TestRuntimeException.java
package demo1;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/*
* ArithmeticException -> 算数异常类
ArrayIndexOutOfBoundsException(间接子类) -> 数组下标异常类
NullPointerException -> 空指针异常
ClassCastException -> 类型转换异常
NumberFormatException(间接子类)-> 数字格式异常
*/
public class TestRuntimeException {
public static void main(String[] args) {
// 观察检测性异常
// FileInputStream fis = new FileInputStream("c:/a.txt");
// java.lang.ArithmeticException 算数异常
int a = 10;
int b = 0;
if (b != 0) {
System.out.println(a/b);
}
// java.lang.ArrayIndexOutOfBoundsException 数组下标越界异常
int[] arr = new int[3];
int num = 3;
if (num >= 0 && num < arr.length) {
System.out.println(arr[num]);
}
// java.lang.NullPointerException
String str = null;
if (str != null) {
System.out.println(str.length());
}
// java.lang.ClassCastException
Exception ex = new Exception();
if (ex instanceof IOException) {
IOException ie = (IOException) ex;
}
// java.lang.NumberFormatException
String s = "12be";
if (s.matches("\\d+")) {
System.out.println(Integer.parseInt(s));
}
System.out.println("运行程序结束");
}
} try { a; b; // 可能产生异常的语句 c; } catch (Exception e) { e; } finally { f; }- 当没有产生异常的时候,程序的执行流程是:a b c f
- 当产生异常时,程序的执行流程是: a b e f public class TestExceptionCatch { public static void main(String[] args) { // 声明引用指向本类的对象,用于读取文件中的内容 FileInputStream fis = null; try { // 随时可能产生文件找不到y异常对象, new FileNotFoundException() fis = new FileInputStream("d:/a.txt"); } catch (FileNotFoundException e) { // 打印异常的名称、异常原因、异常的位置等信息 e.printStackTrace(); } try { fis.close(); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { // System.out.println("输出"); e.printStackTrace(); } }}```- TestFinally.java
```java
package demo3;public class TestFinally { public static void main(String[] args) { int a = 10; int b = 0; try { System.out.println(a/b); } catch (Exception e) { e.printStackTrace(); return ; } finally { System.out.println("无论是否发生异常都会执行"); } System.out.println("程序结束"); }}``` java.lang.ArithmeticException: / by zero 无论是否发生异常都会执行 at demo3.TestFinally.main(TestFinally.java:9) import java.io.IOException; public class A { public void show() throws IOException{ System.out.println("A"); } } ``` SubA.java ```java package demo4; import java.io.IOException; import java.io.FileNotFoundException; import javax.print.PrintException; public class SubA extends A{ @Override // public void show() throws IOException { // public void show() throws FileNotFoundException { // public void show() throws PrintException { // public void show() throws Exception { public void show() { } } ``` >显然不能抛出更大的异常public class Person { private String name; private int age; public Person() { super(); } public Person(String name, int age) throws Exception { super(); setName(name); setAge(age); } 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 < 150) { this.age = age; } else { // System.out.println("年龄不合理"); // 手动产生一个异常对象并抛出 // throw new Exception(); throw new AgeException("年龄不合理!"); } System.out.println("产生异常的效果"); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; }}```- AgeException.java
```java
package demo5;public class AgeException extends Exception { /** *
*/
private static final long serialVersionUID = 1L; // 自定义无参的构造方法 public AgeException() { } // 自定义使用字符串作为参数的构造方法 public AgeException(String msg) { super(msg); }}```- TestPerson.java
```java
package demo5;public class TestPerson { public static void main(String[] args) { Person p = null; try { p = new Person("张三", -12); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(p); }}``` demo5.AgeException: 年龄不合理! null at demo5.Person.setAge(Person.java:33) at demo5.Person.<init>(Person.java:15) at demo5.TestPerson.main(TestPerson.java:8)public class Student { private String name; private int id; public Student() { super(); } public Student(String name, int id) throws IDException { super(); setName(name); setId(id); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) throws IDException { if (id > 0) { this.id = id; } else { // System.out.println("学号不合理"); throw new IDException("学号不合理"); } System.out.println("结束"); } @Override public String toString() { return "Student [name=" + name + ", id=" + id + "]"; }}```- IDException.java
```java
package demo6;public class IDException extends Exception { /** *
*/
private static final long serialVersionUID = 1L; public IDException() { } public IDException(String msg) { super(msg); }}```- TestStudent.java
```java
package demo6;public class TestStudent { public static void main(String[] args) throws IDException { /*Student stu = null; try { stu = new Student("小王", -5); } catch (IDException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ Student stu = new Student("小王", -5); System.out.println(stu); }}``` Exception in thread "main" demo6.IDException: 学号不合理 at demo6.Student.setId(Student.java:30) at demo6.Student.<init>(Student.java:14) at demo6.TestStudent.main(TestStudent.java:14)此处有一点要注意,在案例一的TestPerson中,在main函数中,我们使用try....catch语句,异常由内部进行处理,会打印后面的语句
而在案例二的TestStudent中,我们将异常直接交给main函数处理,也就是交给虚拟机处理,所以并不会执行后面的语句
最后简单介绍一下throws和throw的区别
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。