java异常处理
java中的异常分为:编译时检查性异常(checked exception)和非检查性异常(unchecked exception,又称作运行时异常)。
这两者有什么区别呢?,举例说明:
编译时检查行异常:最经常遇到的就是IO异常和SQL异常,对于检查性异常,程序必须进行处理,否则编译器会指出错误。这里就需要我们进行异常catch,并进行相应的处理。
运行时异常:就是RuntimeException,比如:被零除等系统错误、数组下标超范围、空指针异常等,这些有系统进行检测,程序可以不进行处理,交由系统进行缺省处理,当然,必要时,可以对其进行处理。
RuntimeException异常在java.lang包下:
下面是由java虚拟机提供的运行时异常:
AnnotationTypeMismatchException,
ArithmeticException,
ArrayStoreException,
BufferOverflowException,
BufferUnderflowException,
CannotRedoException,
CannotUndoException,
ClassCastException,
CMMException,
ConcurrentModificationException,
DOMException,
EmptyStackException,
EnumConstantNotPresentException,
EventException,
IllegalArgumentException,
IllegalMonitorStateException,
IllegalPathStateException,
IllegalStateException,
ImagingOpException,
IncompleteAnnotationException,
IndexOutOfBoundsException,
JMRuntimeException,
LSException,
MalformedParameterizedTypeException,
MirroredTypeException,
MirroredTypesException,
MissingResourceException,
NegativeArraySizeException,
NoSuchElementException,
NoSuchMechanismException,
NullPointerException,
ProfileDataException,
ProviderException,
RasterFormatException,
RejectedExecutionException,
SecurityException,
SystemException,
TypeConstraintException,
TypeNotPresentException,
UndeclaredThrowableException,
UnknownAnnotationValueException,
UnknownElementException,
UnknownTypeException,
UnmodifiableSetException,
UnsupportedOperationException,
WebServiceException
我们平时常见的异常主要有五大异常:
ClassCastException(类转换异常)
IndexOutBoundsException(数组越界异常)
NullPointException(空指针异常)
ArrayStoreException(数据存储异常,操作数据类型不一致)
BufferOverflowException(IO操作异常)