Java series: "Java Core technology Volume One" study notes, CCHAPTER11 exceptions

Source: Internet
Author: User
Tags stack trace throwable

11.1.1 Exception Classification If a program appears runtimeexception, then it must be your problem.


11.1.2 Declaration has detected an exceptionIf a subclass overrides a method of the parent class, the check exception declared in the subclass method cannot be more general than the exception declared in the superclass method.
11.1.4 Create exception class all custom exception classes should implement two constructors, one is the default constructor, and the other is a constructor with a string parameter;

11.2 Catching exceptions in general, you should catch exceptions that know how to handle them, and continue to pass on exceptions that do not know how to handle them.

11.2.1 captures multiple exceptions in Java7, the same catch statement can catch multiple exceptions, such as:
  
 
  1. try{
  2. ....
  3. }catch(FileNotFoundException | UnknownHostException){
  4. ...
  5. }
This method is only required if the caught exception class does not have a parent-child relationship with each other.

11.2.2 throws an exception again or causes the exception chain to set the original exception to the new exception.
  
 
  1. try{
  2. ...
  3. }catch(Exception e){
  4. Throwable se = new ServetException("database error: ");
  5. se.initCause(e);
  6. throw se;
  7. }
When you catch an exception, you can use throwable se = E.getcause () to get the original exception.
11.2.4 a try statement with a resource you know, this is the same as the using statement in C #.
 
   
  
  1. try(Resources res = ){
  2. work with this res
  3. }
The Res.close () statement is automatically called when the try block exits. (in C #, call the Res.dispose () method)
For example, the following two codes are equivalent
  
 
  1. FileInputStream fs=null;
  2. try{
  3. fs = new FileInputStream("D:\test.txt");
  4. fs.xxxxx
  5. }
  6. finally{
  7. if(fs != null) fs.close();
  8. Span class= "pun" >}
 
   
  
  1. try(FileInputStream fs= new FileInputStream("D:\test.txt")){
  2. fs.xxxx
  3. }
Obviously, the second piece of code is much simpler.
And you can define multiple resources in a try block, separated by commas, and the compiler automatically calls the close () method of all resources.
11.2.5 Analyzing stack trace information
  
 
  1. Throwable t = new Throwable();
  2. StackTraceElement[] frames = t.getStackTrace();
  3. for(StackTraceElement frame : frames){
  4. //xxxxx
  5. }
Through the stacktraceelement can obtain the corresponding file name, the wrong line number, class name, method name and other information, which can be more convenient to analyze and debug a problem.
The following method can get stack information for all threads of the current application, which was not seen in C # before, which also shows the importance of learning different languages from the side.
  
 
  1. Map<Thread, StackTraceElement[]> map = Thread.getAllStackTrace();
  2. for(Thread t: map.keys()){
  3. StackTraceElement frames = map.get(t);
  4. analyze frame
  5. }

Several typical methods of stacktraceelement
  
 
  1. String getFileName();
  2. int getLineNumber();
  3. String getClassName();
  4. String getMethodName();
  5. boolean isNativeMethod();
  6. String toString();

11.3 Tips for using exceptions 1) exception handling can not replace the simple test, that is, do not use the exception to do the program to determine the branch; 2) to properly use the exception hierarchy, throw or capture, to try to use a class that can express a specific purpose, which will have better readability, Do not use throwable at all times, so that the readability is poor, 3) in the detection of errors, "harsh" than laissez-faire better, such as Stack.pop () is the return of NULL better or run out emptystackexception better? The author thinks the latter is better. 4) Don't be shy about passing exceptions. If you call a method that runs out of an exception, such as the Fileinputsteam constructor or the ReadLine method, these methods instinctively capture these possible exceptions. Passing exceptions is better than catching exceptions.
  
 
  1. public void readStuff(Sting fileName) throw s IOException{
  2. InputStream is = new FileInputStream(fileName);
  3. //xxxx
  4. }
Let high-level users know that an error has occurred, or that it is more appropriate to discard unsuccessful commands.
Then the above two points can be summed up as: Early throw, late capture.




From for notes (Wiz)

Java series: "Java Core technology Volume One" study notes, CCHAPTER11 exceptions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.