Java study notes (11) - Exception Handling

Exceptions are some errors in the program, but not all errors are abnormal and wrong sometimes be avoided.

For example, your code less a semicolon, then the result is running out tips are wrong java.lang.Error; if you use System.out.println (11/0), then you are because you have done with the divisor 0 It will throw java.lang.ArithmeticException exception.

Java Exception is divided into the following categories:

  1. Checked exception : the most representative of checked exception is an exception due to user error or a problem, it is the programmer unpredictable. For example when you want to open a file does not exist, an exception occurs, the exception can not simply be ignored at compile time.
  2. Runtime exceptions anomaly is likely to be a programmer to avoid the run-time exception: In contrast to the examination of the anomaly, the exception can be ignored at compile-time run-time.
  3. Error : error is not abnormal, but from the programmer to control the problem. Errors are usually ignored in the code. For example, when a stack overflow, an error occurs, they can not check at compile.

All exception classes inherit from java.lang.Exception class is a subclass. Exception class is a subclass of Throwable class. In addition Exception class, Throwable there is a subclass of Error. Between them into the following:

Exception class

Are an exception, the exception can be handled from the Exception class that inherits dealt program continues to run. To inherit from Error class is wrong, the error can not be processed at runtime, you can only modify the code logic. Exceptions, such exceptions can be handled in the program inherited from the Runtime class is running, it can not handle. An exception must be handled in the code rather than running. Otherwise, the compiler will complain.

Java exception handling in the way

Java exception handling in the main are the following:

  1. Use throwthrow an exception specified in the specified method. For example, throw IOException();it throws an exception in the IO method
  2. Use throwsthe exception is thrown to the caller process. Used in the function declaration. When a method declaration can throw multiple exceptions, if there are multiple exceptions inheritance, you only need to throw an exception to the parent class. If a parent class does not throw an exception, when rewriting subclass ancestor method can not be used in this way thrown
  3. try...catch Handle exceptions.

When using try handling exceptions should be noted:

  • If there are multiple exceptions catch captured, and the inheritance relationship between abnormal, you must handle EDITORIAL class, parent class in the back

Exception common methods

Throwable It defines three exception handling method:

  1. String getMessage(): For more information on the abnormal return
  2. String toString(): Returns the exception brief information
  3. void printStackTrace(): Print information call the exception

These anomalies generally try ... catch is used, e.g.

try
{
    //do something
}catch(Exception e){
    e.printStackTrace();
}

finally keyword

Regardless of whether an exception occurs, finally the code will be executed. General finally write code to release resources, such as the release of the file object. It should be noted, finally will change the order of execution of return, no matter where in return, will last execution finally in return

try{
    //do some thing
    return;
}catch(Exception e)
{
    return;
}
finally{
    return; //会执行这个 
}

return;

Custom exception class

Custom anomaly Note:

  • Exception classes must inherit from Throwablethe class, if you want to define checked exception, needs to inherit Exceptionthe abnormal, you need to be defined to inherit runtime RuntimeException.
class MyException extends Exception{
}

Suppose we define an exception class that represents withdrawals exception, when the number of withdrawals of less than 1,000 Times abnormal, the user is prompted to take the ATM, you can write

class TooLittleMoneyException extends Exception
{
    private int money;
    private String message;

    TooLittleMoneyException(int money){
        message = "" + money + "太少,请到ATM自助取款机去取";
    }

    String getMessage(){
        return message;
    }
}

//取钱方法
//打开交易通道
//校验账户是否合法
try{
    if (money < 1000){
        throw TooLittleMoneyException(money);
    }

    //取钱,并在对应账户中减少相应的金额
}catch(TooLittleMoneyException e){
    System.out.println(e.getMessage());
}finally{
    //关闭交易通道
}

Guess you like

Origin www.cnblogs.com/lanuage/p/11109281.html