JAVA --- abnormalities and packages

table of Contents

abnormal:

Abnormal origin:

Division problem:

Abnormal system

Abnormal system features:

Abnormal benefits:

Exception handling principles:

For multi-processing abnormalities

Custom exception

How to define anomalies

Inheritance Exception reasons:

throw and thorws usage:

RuntimeException

For exceptions in two ways:

Abnormal Application

Exception handling in three formats:

Knowledge points:

Abnormality reflected in the child-parent class overrides

package:


abnormal:

Program at run time is not normal circumstances, is a description of the problem, the problem is encapsulated object.

Abnormal origin:

The problem is in real life a specific thing, can also be described by the form of java classes and packaged as an object, in fact, the situation is not normal java to reflect the formation description

 

Division problem:

1. For serious problems, java class described by Error

  • For Erroe generally do not write specific code to process it

2. For non-critical problems, java is described by the Exception class

  • Exception may be used for targeted treatment processes

Exception Error or whether it has some common content

 

Abnormal system

Throwable

---Error

  • Usually major problems, such as: class does not exist or running out of memory, etc.
  • Do not write the code for its processing

---Exception

  • In the case together with the runtime operation that appears, you can try catch finally

Exception and Error subclasses are based on the name of the parent class name as a suffix

 

Abnormal system features:

Anomalies in the system all the classes and objects are created with disposable nature, that can be operated throws and throw keywords, only abnormal system with this feature

Exception handling:

try
{
    需要被检测的代码
}
catch(异常类 变量)
{
    处理异常的代码;(处理方式)
}
finally
{
    一定会执行的语句;
}

Abnormal benefits:

1. The problem encapsulation

2. Problem normal process code and the code phase separation, easy to read

 

Exception handling principles:

1, the process in two ways, or the try throws

2. When calls to the function throws an exception, several throws, to handle several, corresponding to a plurality of try catch

A plurality 3. catch, the catch into the bottom of the parent

Within 4.catch, we need to define a targeted approach, not simply to define the output statement, or else do not write

 

Abnormal objects captured were common method of operation

String getMessage(); //获取异常信息
//在函数上声明异常,便于提高安全性,让调用处进行处理,不处理编译失败
class Demo
{
    int div(int a,int b)throws Exception //在功能上通过throws的关键字声明了该功能有可能出现的问题
    {
        return a/b;
    }
}

 

For multi-processing abnormalities

  • When unusual statement, the statement recommended that more specific exceptions, such treatment can be more specific (an exception there are many built-in exceptions)
  • The other statement a few exceptions, there are several on the corresponding catch block, do not define the excess catch block
  • If an exception occurs inheritance plurality catch block, the parent class exception catch block on the bottom, as if on top, all abnormal abnormality advances to the parent

 

Because there will be issues specific to the project, and these problems have not been described and packaged java objects, it can be a problem peculiar to customize these specific issues in accordance with the ideas of java package of the problem of abnormal package

 

Custom exception

When processing action occurs throw objects thrown within a function, then it must give the corresponding, either try catch treatment, either in a statement on function so that the caller process

Generally abnormal in function, declare the function

The definition of class inheritance Exception or RuntimeException

1. In order for the custom class includes a disposable of

2. allow such methods have in common abnormal operation

 

 

How to define anomalies

When information is to define the custom exception you may have been defined using the parent class function

According to the java object-oriented thinking, the program will appear in the package-specific issues

//自定义异常,必须是自定义类继承Exception
class FuShuException extends Exception
{
    FuShuException(String msg)
    {
        //因为继承于Exception,父类中已经把异常信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类通过super语句,也可以直接通过getMessage方法获取自定义的异常信息
        super(msg);   
    }
}

class Demo
{
    int div(int a,int b)throws FuShuException
    {
        if(b<0)
            throw new FuShuException("出现除数是复数的情况"); //手动通过throw关键字抛出一个自定义异常对象
        return a/b;
    }
}

Inheritance Exception reasons:

Abnormal system has a characteristic: Because exception classes and abnormal objects are thrown, they may throw all have sex, this can throw Throwable of this system is a unique feature, only the total system of classes and objects can put throws and throw operation

 

throw and thorws usage:

throw is defined in function, for the object thrown

functions defined on the throws, a throw exception class that is thrown with a comma plurality

When the function contents throw thrown objects, try not be treated, must be declared on the function, attention, RuntimeException except, that is, within the function if RuntimeException thrown an exception, you can not declare a function

 

If the abnormal function declaration, the caller needs to be processed, the processing method may throws can try

 

 

RuntimeException

  • Exception has a special subclass RuntimeException abnormal runtime exception
  • If Thrown in content function, the function can not declare, as compiled by
  • If the exception is declared in the function, the caller can not be processed, as compiled by
  • The reason is not used in the function declaration, because the caller does not need to let the process when the exception occurs, you want the program to stop, because at runtime, there have been cases unable to continue operations, and hopefully stop the program, the code amendments
  • Custom exception if the exception occurs, the operation can not proceed, let custom exception inherited RuntimeException

 

For exceptions in two ways:

The abnormality detection 1. Compiled

     The exception at compile time, if there is no treatment (no cast and no try), the compilation fails

     The exception is identified, it can be processed on behalf of

2. Not detected abnormality (abnormality, RuntimeException subclasses and runtime) Compiled

     At compile time, does not require processing, the compiler does not check

     The anomaly occurs, treatment is not recommended, so that the program stops. Code needs to be amended

Abnormal Application

public void method() throws NoException
{
    try
    {
        连接数据库;
        数据操作; //throw new SQLException();
    }
    catch(SQLException e)
    {
        会对数据库进行异常处理;
        throw new NoExeption  //将处理后的结果抛出
    }
    finally
    {
        //这一步很重要,如果每次连接不成功或处理完数据后不关闭连接,会使链接数满,其他也就连接不上了
        关闭数据库;
    }

}
  • finally defined in the source code is usually closed, because the resource must be released
  • finally only one case is not executed when the execution System.exit (0); finally will not be executed

Exception handling in three formats:

//第一种
try
{
}
catch()
{
}
//第二种格式
try
{
}
catch()
{
}
finally
{
}
//第三种格式,不处理异常,需要将异常抛给其他层来处理
try
{
}
finally
{
}

Knowledge points:

  • catch is used to treat abnormal, if not catch on behalf of not being processed, exception, must be declared if the abnormality is detected
  • When the caught exception, this function can not handle, the catch may continue thrown
  • If you can not handle the exception, but it does not belong to the abnormal function occurs after an exception can be converted, in the throws and functionally related abnormalities.
  • After an exception can be handled or when you need to create problems and anomalies associated with providing this functionality out when the caller knows, and processing, exception processing can also be captured, converted new exception
  •  Why not just use runtimeexception, because users see this name does not know anything unusual happened, so inheritance runtimeecception, played a meaningful name for the inherited class, can well identify this anomaly
  • Try-catch can be a good code and the normal flow separated from exception handling code, it is easy to read, is more convenient than if

 

Abnormality reflected in the child-parent class overrides

1. When cover subclass parent, if the parent class throws an exception, then the method of covering the subclass, the only exception is thrown, or a subclass of the parent class, other classes may not throw exceptions

2. If a plurality of parent class method throws exception, when subclasses override this method, only a subset of the parent class throws exception

3. If the parent class or interface methods are no exception is thrown, then the subclass in the overlay method, nor can throw an exception if the subclass method exception occurs, it must be processed try, absolutely can not throw

 

package:

  • Class files classified management
  • Namespace to provide a multi-layer class
  • Write in the first line of the program file
  • The full name of the class name is the package name. Class name
  • Also a bag package

Access between the package and the package, the package is accessed in the class as well as members of the class, you need to modify public

Subclass different packages can also directly access the parent class members modified Protected rights

Permissions can be used between the package and the package are only two, public protected

You can use import to introduce packet address, so that you can write the class name directly, eliminating the need to write the package name

Published 144 original articles · won praise 17 · views 30000 +

Guess you like

Origin blog.csdn.net/lclcsdnblink/article/details/104560821