JAVA认证 百分网手机站

java运行常见异常举例

时间:2017-10-16 11:49:49 JAVA认证 我要投稿

java运行常见异常举例

  下面小编为大家总结了几个JAVA中常见的RuntimeException,希望对你们有所帮助:

  NullPointerException:空指针异常类

  示例1:

  package com.darkmi.basic;

  public class Test {

  public static void main(String[] args) {

  System.out.println(toUpper(null));

  }

  public static String toUpper(String str){

  return str.toUpperCase();

  }

  }

  异常信息如下:Exception in thread “main” java.lang.NullPointerException

  at com.darkmi.basic.Test.toUpper(Test.java:11)

  at com.darkmi.basic.Test.main(Test.java:6)

  ArrayIndexOutOfBoundsException:数组下标越界异常

  示例2:

  package com.darkmi.basic;

  public class Test {

  public static void main(String[] args) {

  int[] a = {0,1,2,3};

  System.out.println(a[4]);

  }

  }

  异常信息如下:

  Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4

  at com.darkmi.basic.Test.main(Test.java:7)

  ArithmeticExecption:算术异常类:

  示例3:

  package com.darkmi.basic;

  public class Test {

  public static void main(String[] args) {

  int a = 10 / 0;

  System.out.println(a);

  }

  }

  异常信息如下:

  Exception in thread “main” java.lang.ArithmeticException: / by zero

  at com.darkmi.basic.Test.main(Test.java:6)

  ClassCastException:类型强制转换异常

  示例4:

  package com.darkmi.basic;

  public class Test {

  public static void main(String[] args) {

  testParse(“aaa”);

  }

  public static void testParse(Object str){

  Integer i = (Integer)str;

  }

  }

  异常信息如下:

  Exception in thread “main” java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

  at com.darkmi.basic.Test.testParse(Test.java:10)

  at com.darkmi.basic.Test.main(Test.java:6)

【java运行常见异常举例】相关文章:

1.java运行常见异常类型

2.Java异常归类汇总

3.Java编程常见问题汇总

4.Java常见的码块有哪些

5.Java程序开发与运行环境

6.Java编写计算器的的常见做法

7.JAVA常见编码问题解决方法

8.2017年java常见面试题及答案