Java: class7 exception handling, interfaces

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43336822/article/details/95599019

Exception Handling

An abnormal divided ERROR (can not handle exceptions) and Exception (exception handling may be): Throwable-> ERROR, Exception
and Exception divided compile and run Abnormal Abnormal.
Abnormal 1. Run
1) .NullPointerException null exception
2) .ArithmeticException divided by abnormal 0
3) .InputMismatchException input abnormal
4) .ArrayIndexOutOfBoundsException array subscript bounds exception
5) InterruptedException interrupt exception
2. Compile the exception of
1). FileNotFoundException file not found abnormalities

two,

try{
                      //一条或多条可能会发生异常的代码块
                        }catch( ArrayIndexOutOfBoundException e)
                                {
                                   // e.printStackTrace();
                                    System.out.println("下标越界");
                       }catch(ArithmeticException e)
                                {
                                    System.out.println("除零错误");
                                            }

1. a try corresponds to one or more of the catch, catch an exception should be specific about the care, do not care about no abnormal refinement points, or look messy
in general do not care to write to a common anomaly: catch (Exception e) {} , i.e. accepted by the parent class, reflects upward modeling (modeling up: a reference object of a derived class of the base class)
2 to a catch behind the catch of the catch does not continue, i.e. try earlier code has been caught exception, not the back of the continued implementation of the statement.

3.finally {}: catch after the catch or statements must not be performed (exception statement will be executed, whether occurred), a try corresponds to a finally.
It can be applied to a file, if the file that is whether the mission failed, all files can be closed.

4.try ​​block return calls before finally block, returning from finally. That is, no matter what happens, finally it will certainly be executed.

Three, throws: throw exception class

   public void fun() throws Exception{
                int a=10/0;}
  
   public static void main(Stirng[]args){
            try{
                        fun();

                }catch(Exception e){
                         e.printStackTrace();
                         }
         }

Four, throw: throw exception objects

  public static void fun(int[] arr){
          if(arr == null)
              throw new NullPointerException();
}

Fifth, the custom exception
class MyException extends Exception ()

Sixth, exit
1.return

2.System.exit (): main program exits from the main function, the display in (). // default 0 for normal exit, negative abnormal returns.

interface

  1. Interface: Constant public static final
  2. Interface no constructor, not instantiate
  3. The method can abstract interface, i.e., can not have the function thereof.
  4. Classes and class inheritance is extends relationship.
  5. Abstract classes and interfaces are all implemented method implements interface.
  6. Class implements an interface, an abstract interface methods must be fully realized.
  7. Interface and Interface: inheritance. Interface may be implemented multiple class C implements A, B, D, C-based case all abstract methods must be implemented interfaces.
  8. Thread.sleep (10000): Process to sleep 10 seconds

Guess you like

Origin blog.csdn.net/qq_43336822/article/details/95599019