Tag: Get line number str condition source throws OWA method expression
1. Cause of the error:
Input error device error physical limit code error
2. Exception classification
Total: Throwable->error
->exception->ioexception
->runtimeexception:
Exceptions that inherit runtimeexception exceptions include:
The type conversion of the error;
Null pointer
Access out of bounds
Exceptions that do not inherit runtimeexception exceptions include:
Attempting to open a file that does not exist
Attempting to read data at the end of a file
An attempt was made to find a class object based on a given string, but the object does not exist
Two exceptions in C + +: The Logic_error class is equivalent to a runtimeexception representation of a logic error in a program
The Runtime_error class is equivalent to a non-runtimeexception exception. Base class for exceptions that are thrown due to unpredictable causes
3. Declare the exception to check:
Public FileInputStream (String name) throws Filenotfountexception
The exception is thrown:
1) Call a method that throws a checked exception
2) errors are found during the program's operation and throws a checked exception (throw) with throw
3) Program error throws an unchecked exception
4) Java Virtual machine error in run-time library
The first line of the method should declare an exception that the method might throw
Any code should have the potential to throw an exception, so the exception to inheritance and error does not need to be declared.
The same runtimeexception exception should not be declared, because the class exception should be modified in the code to avoid the occurrence of
If a subclass method overrides an object of the parent class, the subclass throws an exception that is more specific than the exception thrown by the superclass, which means that the exception that is declared in the parent class method is more common than the exception in the Subclass object method
5. Throw an exception throws new Eofexception ()
String readdate (Scanner in) throws Eofexception
{
while (true)
{
if (In.hasnext ())
{
if (n < len)
{
String err = "Throw exception";//You can customize the thrown string
throw new Eofexception (ERR);
throw new Eofexception ();
}
}
}
Return
}
C + +: Java can only throw throwsable subclass objects. C + + can throw objects of various classes
6. Define an exception class to inherit the exception base class
Class Fileformatexception Extens IOException
{
Publuc fileformatexception () {}
Publuc fileformatexception (String Err)//overloaded constructors can print error messages
{
Super (ERR);//The ToString of superclass Throwable will print error message
}
}
//////////////////////
Java.lang.Throwable
Throwable ()
Throwable (String message)
String getMessage ()/Get details of the Throwable object
//////////////////////
7. Capturing exception information
try{
...
}
catch (Exception E1)
{
...
E1.printstacktrace ();//Print error stack
}
catch (Exception E2)
{
...
E2.printstacktrace ();//Print error stack
}
catch (Exception E3)
{
...
E3.printstacktrace ();//Print error stack
}
If the code in the method throws an exception type that is not declared in an early catch, the method exits
8. Throw the exception and exception chain again
1. Throw an exception again in the catch
2. Wrapping the exception chain and getting the original exception reason
try{
...
}
catch (SQLException e)
{
Throwable se = new servletexception ("Database Err");
Se.initcause (e);
Throw SE;
}
When you get an exception, you can use Throwable e =se.getcause (); re-get the original cause of the exception
9.finally//Same as C #
At finally only return will have unexpected results because the return value inside the finally will overwrite the return value in the Try Method
10. Try with a resource
Try (Resource res = ...)
{
}
The Res.close () method is called automatically whenever a try exits, regardless of whether an exception occurs
11. Analyzing Stack Traces
Printstacktrace ();
Getstacktrace ();//Returns an Stacktraceelement array
The array includes the file name, line number, class name, method name
static Thread.getallstacktrace (); Track all line stacks
(Original book 489 page Throwable API)
12. Tips for Using exceptions
1) exception handling is not a substitute for simple testing (time consuming)
2). Do not refine the exception excessively
Bad habits:
for (i = 0; i <; i++)
{
try{
...
}catch (Exception e) {
...
}
}
Change to: (The above example is too detailed for each process)
try{
for (i = 0; i <; i++)
{
...
}
}catch (Exception e) {
...
}
3) using the exception hierarchy
Do not just throw runtimeexception exceptions. Should look for a more appropriate subclass exception
Do not capture only throwable exceptions. It will be difficult to maintain the source code
4) do not suppress anomalies
5) When detecting errors, "harsh" is better than laissez-faire.
6) Don't be shy about passing the exception
5) 6) = = "Using the abnormal chain to throw early, late capture,
13.assert assertion (allows a check statement to be inserted during testing, and the detection statement will be moved automatically when the code is published)
Assert condition: Assert condition: expression
Assertionerror exception will be thrown when the assertion condition is flase
In the second form, the expression is converted into a message string in the Assertionerror constructor
Example: Assert x>0 or assert x>0:x; The value of X is passed into the Assertionerror object
C + +: assert (x >= 0); The fail condition will be automatically printed
= = "Java:assert x > 0:" x > 0 ";
14. Enable and disable assertions
15.java provides three systems for handling errors: Throwing exceptions, logging, using assertions
Case of using assertions:
Assertion failure is fatal, unrecoverable error
Only for development testing phase
Assertion API original book (496 pages)
16. Logging
17. Debugging Skills
Java Learning Note (Core Java) 7 assertions and exceptions