谷旭网

当前位置:主页 > 常识 >

javaexception exception是所有异常的

javaexception exception是所有异常的

时间:2023-11-17 04:28:57 来源: 作者:admin

  exception是所有异常的父类,在 java.lang包里面

  Exception java.lang中

  ~~~~~~~~~~

  java中的异常多了,但是常见的异常也就如下几种:

  import java.nio.*;

  import java.util.*;

  class testexception {

  public static void main(string[] args) {

  testexception t=new testexception();

  t.testindexoutofboundsexception();

  }

  // 1。arithmeticexception异常。 被除数为0时触发

  public void testarithmeticexception() {

  int a=10;

  int b=0;

  int c=a/b;

  }

  // 2。arraystoreexception异常。 数组类型不正确时触发

  public void testarraystoreexception() {

  object x[]=new string[3];

  x[0]=new integer(0);

  }

  // 3。bufferoverflowexception异常。

  public void testbufferoverflowexception() {

  int cap=10;

  bytebuffer bf=bytebuffer.allocate(cap);

  system.out.println(bf);

  for(int i=0;i bf.put((byte)i); } system.out.println(bf); // cause exception bf.put((byte)10); } // 4。bufferunderflowexception异常。 public void testbufferunderflowexception() { int cap=10; bytebuffer bf=bytebuffer.allocate(cap); system.out.println(bf); for(int i=0;i bf.put((byte)i); } system.out.println(bf); // cause exception bf.get(); } // 5。classcastexception异常。 类的类型不正确的cast的时候触发。 public void testclasscastexception() { object x=new integer(0); system.out.println((string)x); } // 6。emptystackexception异常。 堆栈为空的时候触发 public void testemptystackexception() { stack t=new stack(); t.push (new object()); t.pop(); // cause exception t.pop(); } // 7。illegalargumentexception异常。 调用方法时传入不合法的参数的时候触发。 public void testillegalargumentexception() { thread.currentthread().setpriority(thread.min_priority); // caush exception thread.currentthread().setpriority(99); } // 8。indexoutofboundsexception异常。 索引超出边界时触发。 public void testindexoutofboundsexception() { list l=new arraylist(); l.add("t"); l.add("a"); l.add("n"); l.add("k"); l.add("s"); collections.swap(l,0,9); } // 9。arrayindexoutofboundsexception异常。 索引超出数组边界时触发。 public void testarrayindexoutofboundsexception() { list v=new arraylist(); v.add(new object()); v.get(0); // cause exception v.get(-1); } // 10。stringindexoutofboundsexception异常。 索引超出字符串边界时触发。 public void teststringindexoutofboundsexception() { string s="hello"; s.charat(-1); } }

  package java.lang

标签: #in #异常 #id

声明:本站内容均来自互联网,不代表本站观点和立场,如侵联删!

相关阅读