JAVA 的checked异常和unchecked异常
- 什么是unchecked异常?
即RuntimeException(运行时异常) 不需要try...catch...或throws 机制去处理的异常
- 列举最常用的五种RuntimeException:
这是JAVA认证考试中最常见的题目,事实上,runtime exception中最常见的,经常碰到的,也就5,6种,如下:
ArithmeticException | int a=0; int b= 3/a; |
ClassCastException: | Object x = new Integer(0); System.out.println((String)x); |
IndexOutOfBoundsException ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException | int [] numbers = { 1, 2, 3 }; int sum = numbers[3]; |
IllegalArgumentException NumberFormatException | int a = Interger.parseInt("test"); |
NullPointerExceptionextends |
- 除了RuntimeException,其他继承自java.lang.Exception得异常统称为Checked Exception,他们有多少种呢?
下面是JDK API中列出的异常类: 除了RuntimeException以外的,都是checked Exception
java.lang.Object java.lang.Throwable java.lang.Exception 所有已实现的接口: Serializable 直接已知子类: 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, JAXBException, JMException, KeySelectorException, LastOwnerException, LineUnavailableException, MarshalException, MidiUnavailableException, MimeTypeParseException, MimeTypeParseException, NamingException, NoninvertibleTransformException, NoSuchFieldException, NoSuchMethodException, NotBoundException, NotOwnerException, ParseException, ParserConfigurationException, PrinterException, PrintException, PrivilegedActionException, PropertyVetoException, RefreshFailedException, RemarshalException, RuntimeException, SAXException, ScriptException, ServerNotActiveException, SOAPException, SQLException, TimeoutException, TooManyListenersException, TransformerException, TransformException, UnmodifiableClassException, UnsupportedAudioFileException, UnsupportedCallbackException, UnsupportedFlavorException, UnsupportedLookAndFeelException, URIReferenceException, URISyntaxException, UserException, XAException, XMLParseException, XMLSignatureException, XMLStreamException, XPathException |
我经常碰到的checked exception:
AWTException,
CertificateException,
ClassNotFoundException,
CloneNotSupportedException,
InterruptedException,
InstantiationException,
IllegalClassFormatExceptionm,
IllegalAccessException,
IOException,
InvocationTargetException,
NoSuchFieldException,
NoSuchMethodException,
ParserConfigurationException,
SOAPException,
SQLException,
TimeoutException,