Think in Java Notes _chapter12_1_exception Basics _ Inheritance and runtimeexception processing 1

Source: Internet
Author: User
Tags ming

Exceptions in Java
  1. Exception exception we can deal with, can be remedied, encountered an exception will not be executed?
    1. Package cn.seven.shengsiyuan.exception;/* * time:2015 April 05 12:24:58, Ching Ming Festival * location:501 *  */public class ExceptionDemo1 {public static void main (string[] args) {//TODO auto-generated method Stubint A = 3;int b = 0;int C = A/ b;//This sentence execution will occur an exception, the following statement will not execute SYSTEM.OUT.PRINTLN ("------"); System.out.println (c);}}

      /*
      Exception in thread "main" java.lang.ArithmeticException:/By zero
      At Cn.seven.shengsiyuan.exception.ExceptionDemo1.main (exceptiondemo1.java:17)

      */
  2. Exceptions in the exception inheritance system-java inherit from the exception class exception into two main categories:
    Checked Exception (non-runtime exception)
    Unchecked Exception (runtime exception runtime Exception)

    Java.langclass throwable    java.lang.Object        java.lang.Throwable all    implemented Interfaces:        Serializable    Direct known subclasses:        

    Java.langclass Exception    java.lang.Object        java.lang.Throwable            java.lang.Exception
    1. Run-time Exception runtimeexception
          Java.lang.Object        java.lang.throwable-            java.lang.Exception                java.lang.RuntimeException                    Javax.management.JMRuntimeException

    2. Non-runtime exception: inherits the exception class, but does not inherit exceptions from the RuntimeException class
  1. RuntimeException exception handling-fixed processes and patterns
    1. Try{}catch (Exception e) {}

    2. Package cn.seven.shengsiyuan.exception;/* * time:2015 April 05 12:24:58, Ching Ming Festival * location:501 *  */public class ExceptionDemo1 {public static void main (string[] args) {//TODO auto-generated method Stubint c = 0;try{int A = 3;int B = 2; C= A/b; SYSTEM.OUT.PRINTLN ("---hello---");} catch (ArithmeticException e) {//assigns the exception object generated by 19 rows to Ee.printstacktrace ();//printstacktrace printing exception information from the Throwable class method} Finally{system.out.println ("finally");} SYSTEM.OUT.PRINTLN ("------"); System.out.println (c);}} /* * 19 rows with an exception, resulting in an object for the exception class ArithmeticException * When an exception occurs, enter the catch block, take over by the catch, and then execute the contents of the Catch block * execution is complete, then execute the SYSO statement *  *  * The focus now is on the 21-row-catch statement process */

    3. Complete exception-handling structure
      try{//the code block of a statement that may have an exception}catch (Exception object) {//For processing after an exception occurs}finally{//the statement that will eventually be executed regardless of whether an exception occurred, preventing some statements from being executed because an exception occurred}

      Package cn.seven.shengsiyuan.exception;/* * time:2015 April 05 12:24:58, Ching Ming Festival * location:501 *  */public class ExceptionDemo1 {public static void main (string[] args) {//TODO auto-generated method Stubint c = 0;try{int A = 3;int B = 0; C= A/b; SYSTEM.OUT.PRINTLN ("---hello---");} catch (ArithmeticException e) {e.printstacktrace ();//printstacktrace printing exception information, methods from the Throwable class}finally{ System.out.println ("finally");//must be executed regardless of whether an exception occurred}//system.out.println (c); The question is why is c=0 still output here? Because of c=a/b; statement exception, C no assignment statement}}

  2. If there are multiple exceptions in the program, only specific exceptions will be handled by the particular catch.

Think in Java Notes _chapter12_1_exception Basics _ Inheritance and runtimeexception processing 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.