JAVA认证

java运行常见异常类型

时间:2024-07-11 10:16:25 JAVA认证 我要投稿
  • 相关推荐

java运行常见异常类型

  java运行时常见异常有很多,下面总结了几个JAVA中常见的RuntimeException:

  NullPointerException:空指针异常类

  示例:

  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:数组下标越界异常

  示例:

  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:算术异常类:

  示例:

  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:类型强制转换异常

  示例:

  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运行常见异常类型】相关文章:

常见的广告招牌类型05-07

Java基本数据类型07-09

常见乐器类型的EQ特性04-12

汽车不必维修的10个常见异常07-16

常见出国留学考试类型10-11

Java中基本数据类型与应用08-23

关于中考语文常见的病句类型及例题08-20

常见网络故障类型及诊断方法10-13

中考化学推断题的常见类型与解法08-23

出国留学常见的八种考试类型02-20