我是靠谱客的博主 搞怪蜜粉,最近开发中收集的这篇文章主要介绍常见的运行时异常,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

java.lang.Throwwable

  •  	|--Error:错误,程序中不进行处理
    
  •  	|--Exception:异常,要求在编译程序时,就要考虑到对这些异常的处理
    
  •  			|--编译时异常:在编译期间会出现的异常
    
  •  			|--运行时异常:在运行期间会出现的异常
    
  • 注意:当执行一个程序时,如果出现异常那么异常之后的代码就不再执行
  • 例:
  • 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,会出现空指针异常
    */
public 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{
	
}

最后

以上就是搞怪蜜粉为你收集整理的常见的运行时异常的全部内容,希望文章能够帮你解决常见的运行时异常所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部