java.lang.Throwwable
- 复制代码1
2|--Error:错误,程序中不进行处理
- 复制代码1
2|--Exception:异常,要求在编译程序时,就要考虑到对这些异常的处理
- 复制代码1
2|--编译时异常:在编译期间会出现的异常
- 复制代码1
2|--运行时异常:在运行期间会出现的异常
- 注意:当执行一个程序时,如果出现异常那么异常之后的代码就不再执行
- 例:
- Bank bank = new Bank();
- Customer[] customers = new Customer[5];
- customers[0] = new Customer();
- Sysotem.out.println(customers[0].getFirstName());在没有创建客户时,customer[0]为null,会出现空指针异常
- customers[0].setAccount(new Account(200));
- customer[0].getAccount().withdraw(100);在没有给可以创建客户时,Account为null,会出现空指针异常
*/
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43public class TestException { @Test //常见的运行时异常 //1.数组下标越界的异常 左右两端都能越:ArrayIndexOutOfBoundsException @Test public void test2() { int[] i = new int[10]; System.out.println(i[10]); System.out.println(i[-10]); } //2.算术异常:ArithmeticException @Test public void test3() { int i = 10; System.out.println(i / 0); } //3.类型转换异常:ClassCastException @Test public void test4() { Object o = new Date(); String str = (String)o; } //4.空指针异常:只在引用方法是出现。NullPointerException @Test public void test5() { Person p = new Person(); p = null; System.out.println(p.toString()); String str = new String("AA"); System.out.println(str.length()); } @Test public void test1() { Scanner s = new Scanner(System.in); int i = s.nextInt(); System.out.println(i); } } class Person{ }
最后
以上就是搞怪蜜粉最近收集整理的关于常见的运行时异常的全部内容,更多相关常见内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复