Java Exception
Abnormal Overview
Abnormal is not normal meaning, Java language mainly refers error 2904628156 programs in the operational phase
Throwable (Throwable be thrown)
Java.lang.Throwable class is a Java program any errors or abnormal superclass
There are two main sub-categories:
Error: describes more serious mistakes, not big mistakes solved by programming
Exception: The main error described in more lightweight, can be solved by programming
Throwable Methods
1. getMessage () - Get exception information, returns the string
2. toString () - Get exception classes and exception information, returns the string
3. printStacTrace () - Get exception classes and exception information, and the locations of the anomalies in the program . Back void
4. printStackTrace (PrintStream S) - This method is generally used to save the contents of abnormality in the log file, for inspection
The main classification Exception class
A RuntimeException - exception, also known anomaly based non-detectable runtime
ArithmeticException - an arithmetic exception classes
An ArrayIndexOutOfBoundsException (indirect subclass) - array subscript bounds exception
NullPointerException - null pointer exception
ClassCastException - type conversion exception
a NumberFormatException (indirect subclass) - number format exception
Note:
When the execution of the program in an exception, if there is no manual processing java virtual machine process using the default mode, the default mode is to print the name of abnormality, and the cause of the abnormality occurrence position of an abnormal termination and subsequent code can not be executed
The exception handling runtime
For the vast majority of the number of abnormal operation, the abnormality can be avoided by determining a transmission condition
Abnormal capture
(1) syntax
try { likely sentence block abnormal object; } catch (Exception type reference name) { while branches with a plurality of catch type of object for the current exception trapping structure exception, the exception type remember small exception types in a wide range of above processing block of statements; } the finally { whether or block of statements executed abnormality should occur; }
(2) Notes
When capturing a plurality of structural abnormalities in catch branches, remember small exception type of abnormality on a wide range of the above types
Lazy wording:
catch(Exception e){...}
(3) execution flow
try{ a; b; c; }catch(Exception e){ e; }finally{ f; } h;
When no abnormality, the execution flow of the program are: abcf
when abnormal, the process execution is: abef
The exception thrown
(1) Basic concepts
in some special occasions, when an exception can not be directly processed / Do not want to deal with, then you can transfer exception to the caller of the current method, which is called an exception thrown
(2) syntax
method return value name (parameter list) throws exception type (......)
(3) The method of rewriting principle
1. The method of claim same names, the same list of parameters, the same type of return value, to return from the start permission jdk1.5 subclasses Type
2 can not access becomes smaller, may be the same or larger
3. not greater throw exception
Note:
After the subclasses override the method can choose to throw with the same parent class exception, smaller abnormalities, but can not throw more exceptions, different exception
Custom exception
(1) anomalous origin of the custom
Java official repository, while providing a number of exception classes, but not enough to describe the display of life in all the abnormal situation, when abnormal conditions are not described in official documents appear to require the programmer to custom exception added type described, so that the abnormality information includes specific and more readability
(2) custom exception process
1. Exception custom class that inherits class or subclass Exception
2. The two versions of the constructors, is not a method of configuration parameters, the other parameters do string constructor
Thrown object
throw new exception type ()
E.g:
throw new Exception()
throws and throw the difference between:
throws
1. The method of statement used in the back, with the exception classes
2. multiple exception classes can be followed, separated by commas
3 represents an exception is thrown, the process by the caller to handle
4. throws an exception represents one possibility, not necessarily these exceptions occur
throw
1. A method for use in vivo, with the exception object name is
2 can throw an exception object name
3 represents an exception is thrown by the method of in vivo processing of the statement
4. throw an exception is thrown, the throw execution We must throw some abnormality
And finally the role of the characteristics of the face questions
finally features
1. The control of finally seeing the body will perform
2. Special circumstances: Before performing to finally jvm withdrew (such as System.exit (0))
finally the effect of
1 to release the resources, will see a stream IO operations and database operations
finally face questions related to
the difference between 1. final, finally and finalize the
2. If you catch a return statement there, what finally code will execute it?
3. If the will, before I ask a return or return
Abnormal Overview
Abnormal is not normal meaning, Java language mainly refers error 2904628156 programs in the operational phase
Throwable (Throwable be thrown)
Java.lang.Throwable class is a Java program any errors or abnormal superclass
There are two main sub-categories:
Error: describes more serious mistakes, not big mistakes solved by programming
Exception: The main error described in more lightweight, can be solved by programming
Throwable Methods
1. getMessage () - Get exception information, returns the string
2. toString () - Get exception classes and exception information, returns the string
3. printStacTrace () - Get exception classes and exception information, and the locations of the anomalies in the program . Back void
4. printStackTrace (PrintStream S) - This method is generally used to save the contents of abnormality in the log file, for inspection
The main classification Exception class
A RuntimeException - exception, also known anomaly based non-detectable runtime
ArithmeticException - an arithmetic exception classes
An ArrayIndexOutOfBoundsException (indirect subclass) - array subscript bounds exception
NullPointerException - null pointer exception
ClassCastException - type conversion exception
a NumberFormatException (indirect subclass) - number format exception
Note:
When the execution of the program in an exception, if there is no manual processing java virtual machine process using the default mode, the default mode is to print the name of abnormality, and the cause of the abnormality occurrence position of an abnormal termination and subsequent code can not be executed
The exception handling runtime
For the vast majority of the number of abnormal operation, the abnormality can be avoided by determining a transmission condition
Abnormal capture
(1) syntax
try { likely sentence block abnormal object; } catch (Exception type reference name) { while branches with a plurality of catch type of object for the current exception trapping structure exception, the exception type remember small exception types in a wide range of above processing block of statements; } the finally { whether or block of statements executed abnormality should occur; }
(2) Notes
When capturing a plurality of structural abnormalities in catch branches, remember small exception type of abnormality on a wide range of the above types
Lazy wording:
catch(Exception e){...}
(3) execution flow
try{ a; b; c; }catch(Exception e){ e; }finally{ f; } h;
When no abnormality, the execution flow of the program are: abcf
when abnormal, the process execution is: abef
The exception thrown
(1) Basic concepts
in some special occasions, when an exception can not be directly processed / Do not want to deal with, then you can transfer exception to the caller of the current method, which is called an exception thrown
(2) syntax
method return value name (parameter list) throws exception type (......)
(3) The method of rewriting principle
1. The method of claim same names, the same list of parameters, the same type of return value, to return from the start permission jdk1.5 subclasses Type
2 can not access becomes smaller, may be the same or larger
3. not greater throw exception
Note:
After the subclasses override the method can choose to throw with the same parent class exception, smaller abnormalities, but can not throw more exceptions, different exception
Custom exception
(1) anomalous origin of the custom
Java official repository, while providing a number of exception classes, but not enough to describe the display of life in all the abnormal situation, when abnormal conditions are not described in official documents appear to require the programmer to custom exception added type described, so that the abnormality information includes specific and more readability
(2) custom exception process
1. Exception custom class that inherits class or subclass Exception
2. The two versions of the constructors, is not a method of configuration parameters, the other parameters do string constructor
Thrown object
throw new exception type ()
E.g:
throw new Exception()
throws and throw the difference between:
throws
1. The method of statement used in the back, with the exception classes
2. multiple exception classes can be followed, separated by commas
3 represents an exception is thrown, the process by the caller to handle
4. throws an exception represents one possibility, not necessarily these exceptions occur
throw
1. A method for use in vivo, with the exception object name is
2 can throw an exception object name
3 represents an exception is thrown by the method of in vivo processing of the statement
4. throw an exception is thrown, the throw execution We must throw some abnormality
And finally the role of the characteristics of the face questions
finally features
1. The control of finally seeing the body will perform
2. Special circumstances: Before performing to finally jvm withdrew (such as System.exit (0))
finally the effect of
1 to release the resources, will see a stream IO operations and database operations
finally face questions related to
the difference between 1. final, finally and finalize the
2. If you catch a return statement there, what finally code will execute it?
3. If the will, before I ask a return or return