当前位置: 移动技术网 > IT编程>开发语言>Java > java异常处理详细介绍及实例

java异常处理详细介绍及实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

java异常层次结构

exception异常

runtimeexception与非runtimeexception异常的区别:

非runtimeexception(检查异常):在程序中必须使用try…catch进行处理,否则程序无法编译。 
runtimeexception:可以不使用try…catch进行处理,但是如果有异常产生,则异常将由jvm进行处理。
比如:我们从来没有人去处理过nullpointerexception异常,它就是运行时异常,并且这种异常还是最常见的异常之一。
出现运行时异常后,系统会把异常一直往上层抛,一直遇到处理代码。如果没有处理块,到最上层,
如果是多线程就由thread.run()抛出,如果是单线程就被main()抛出。抛出之后,如果是线程,这个线程也就退出了。
如果是主程序抛出的异常,那么这整个程序也就退出了。

error类和exception类的父类都是throwable类,他们的区别是:

error类一般是指与虚拟机相关的问题,如系统崩溃,虚拟机错误,内存空间不足,方法调用栈溢等。
对于这类错误的导致的应用程序中断,仅靠程序本身无法恢复和和预防,遇到这样的错误,建议让程序终止。 
exception类表示程序可以处理的异常,可以捕获且可能恢复。遇到这类异常,应该尽可能处理异常,
使程序恢复运行,而不应该随意终止异常。

runtimeexception异常

nullpointexception异常

一般报java.lang.nullpointerexception的原因有以下几种:

1. 字符串变量未初始化;
2. 对象没有用具体的类初始化;

nullpointexception代码如下:

package testnullpointexception;
public class testnullpointexception {
  public static void main (string[] args) {
    string str = null;
    try {
      if (str.equals(null)) {
        system.out.println("true");
      } else {
        system.out.println("false");
      }
    } catch (nullpointexception e) {
      e.printstacktrace();
    }
  }
}

输出:

java.lang.nullpointerexception
  at testnullpointexception.testnullpointexception.main(testnullpointexception.java:6)

arrayindexoutofboundsexception异常

数组下标越界异常,当引用的索引值超出数组长度时,就会发生此异常。

arrayindexoutofboundsexception 代码如下:

package testarrayindexoutofboundsexception;
public class testarrayindexoutofboundsexception {
  public static void main (string[] args) {
    integer[] array = new integer[10];
    try {
      integer temp = array[10];
    } catch (arrayindexoutofboundsexception e) {
      e.printstacktrace();
    }
  }
}

输出:

java.lang.arrayindexoutofboundsexception: 10
  at testarrayindexoutofboundsexception.testarrayindexoutofboundsexception.main(testarrayindexoutofboundsexception.java:6)

arithmeticexception

arithmeticexception是出现异常的运算条件时,抛出此异常。

arithmeticexception代码如下:

/**
 * arithmeticexception
 */

 packet testarithmeticexception;
 public class testarithmeticexception {
  public static void main(string[] args) {
    integer temp = 1;
    try {
      system.out.println(temp/0);
    } catch (arithmeticexception e) {
      e.printstacktrace();
    }
  }
 }

输出:

java.lang.arithmeticexception: / by zero
  at testarithmeticexception.testarithmeticexception.main(testarithmeticexception.java:6)

arraystoreexception

当你试图将错误类型的对象存储到一个对象数组时抛出的异常。

arraystoreexception代码如下:

/**
 * arraystoreexception
 */

 packet testarraystoreexception;
 public class testarraystoreexception {
  public static void main(string[] args) {
    object array = new integer[10];
    try {
      array[0] = "123";
    } catch (arraystoreexception e) {
      e.printstacktrace();
    }
  }
 }

输出:

exception in thread "main" java.lang.arraystoreexception: java.lang.string
  at testarraystoreexception.testarraystoreexception.main(testarraystoreexception.java:6)

numberformatexception

继承illegalargumentexception,字符串转换为数字时出现。

numberformatexception代码如下:

/**
 * numberformatexception
 */
package test;
public class exceptiontest {
   public static void main(string[] args) { 
      string s = "q12";
      integer i = integer.parseint(s);
   }
 }

输出:

exception in thread "main" java.lang.numberformatexception: for input string: "q12"
  at java.lang.numberformatexception.forinputstring(numberformatexception.java:65)
  at java.lang.integer.parseint(integer.java:580)
  at java.lang.integer.parseint(integer.java:615)
  at test.exceptiontest.main(exceptiontest.java:8)

classcastexception

类型转换错误,通常是进行强制类型转换时候出的错误。

classcastexception代码如下:

/**
 * classcastexception 父类赋值给子类,向下转型
 */
package test;
public class exceptiontest {
   public static void main(string[] args) { 
     object obj=new object();
     integer s=(integer)obj;
   }
 }

输出:

exception in thread "main" java.lang.classcastexception: java.lang.object cannot be cast to java.lang.integer
  at test.exceptiontest.main(exceptiontest.java:5)

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网