Java 异常 Exception

来源:互联网 发布:淘宝子账号禁言 认证 编辑:程序博客网 时间:2024/05/02 00:06

1.既然捕获了异常,就要对它进行适当的处理。不要捕获异常之后又把它丢弃,不予理睬

--------------生孩子出来就要对他负责.

 

2.catch语句的用途。catch语句表示我们预期会出现某种异常,而且希望能够处理该异常。异常类的作用就是告诉Java编译器我们想要处理的是哪一种异常

--------------具体问题具体分析,具体处理.

 

还有其他许多异常也可能出现。例如,如果由于某种原因,ResutSet rs = pstmt.executeQuery();返回了null,该怎么办?答案是让它们继续抛出,即不必捕获也不必处理。实际上,我们不能也不应该去捕获可能出现的所有异常,程序的其他地方还有捕获异常的机会??直至最后由JVM处理。

 

JAVA异常可分为3种:

  (1)检查性异常:java.lang.Exception
  (2)运行期异常:java.lang.RuntimeException
  (3)错误:java.lang.Error

 

java.lang.Object  extended by java.lang.Throwable      extended by java.lang.Exception
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AclNotFoundException, ActivationException, AlreadyBoundException, ApplicationException, AWTException, BackingStoreException, BadAttributeValueExpException, BadBinaryOpValueExpException, BadLocationException, BadStringOperationException, BrokenBarrierException, CertificateException, ClassNotFoundException, CloneNotSupportedException, DataFormatException, DatatypeConfigurationException, DestroyFailedException, ExecutionException, ExpandVetoException, FontFormatException, GeneralSecurityException, GSSException, IllegalAccessException, IllegalClassFormatException, InstantiationException, InterruptedException, IntrospectionException, InvalidApplicationException, InvalidMidiDataException, InvalidPreferencesFormatException, InvalidTargetObjectTypeException, InvocationTargetException, IOException, JMException, LastOwnerException, LineUnavailableException, MidiUnavailableException, MimeTypeParseException, NamingException, NoninvertibleTransformException, NoSuchFieldException, NoSuchMethodException, NotBoundException, NotOwnerException, ParseException, ParserConfigurationException, PrinterException, PrintException, PrivilegedActionException, PropertyVetoException, RefreshFailedException, RemarshalException, RuntimeException, SAXException, ServerNotActiveException, SQLException, TimeoutException, TooManyListenersException, TransformerException, UnmodifiableClassException, UnsupportedAudioFileException, UnsupportedCallbackException, UnsupportedFlavorException, UnsupportedLookAndFeelException, URISyntaxException, UserException, XAException, XMLParseException, XPathException


程序代码 程序代码
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 <cap;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 <cap;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);
}
// 11  IOException  找不到文件

}

原创粉丝点击