Java exception (trows and try catch Dead Code), trowscatch

Source: Internet
Author: User
Tags try catch

Java exception (trows and try catch Dead Code), trowscatch
I. Difference between throws and trycatch

(1) For example, publicFileWriter (String fileName) throws IOException {}

Create a FileWrite object in mian.
Importjava. io .*;
Publicclass ShengmingThrows {
Public static void main (String [] args ){
Try {
FileWriter fw = new FileWriter ("k.txt ");
} Catch (FileNotFoundException ex ){}
}
}
(2) Another method:
Importjava. io .*;
Publicclass ShengmingThrows {
Public static void main (String [] args) throws IOException {
// Try {
FileWriter fw = new FileWriter ("k.txt ");
//}
// Catch (IOException e ){}
}
}
Please explain the differences between the two operations. throws only declares an exception and does not handle throws. throws only throws an exception for this type of declaration, but does not capture the exception, let others call its method for processing,

Or continue to throw and throw it to the member functions of the previous function or class .. Try catch is used to capture and Process Code that may encounter exceptions.

Throws only declares an exception and does not handle it.
Of course, try and catch do not have to be handled.
Like code,
Try {
FileWriter fw = new FileWriter ("k.txt ");
}
Catch (IOException e) {} // catch is empty, and it is not processed.

2. What exceptions are thrown, such as capturing multiple exceptions?

(1) The complete code is as follows:

File file = new File ("d: \ a.txt"); // This Is Not A read object and will not throw an exception. Only when it is possible to throw an exception can catch the object, otherwise, the excess catch eclipse will also report the error BufferedReader bf = new BufferedReader (new FileReader (file); // an exception catch (ParseException ex) may be thrown) {// Date d1 = df. parse (tmp_date + t1); the exception thrown by the parse function, so you can write the exception according to the prompts of the function in programming. printStackTrace (); System. out. println ("data parsing exception:" + ex); // log. warn ("****" + ex);} public static void main (String [] args) {DateFormat df = new SimpleDateFormat ("yy-MM-dd HH: mm: ss "); String t1 =" 07:30:45 "; String t2 =" 08:32:46 "; String tmp_date =" "; try {File file = new File (" d: \ a.txt "); // This is not read and will not throw an exception. Only, it is possible to throw an exception to write catch, otherwise, the excess catch eclipse will report the error BufferedReader bf = new BufferedReader (new FileReader (file); Date d1 = df. parse (tmp_date + t1); Date d2 = df. parse (tmp_date + t2); // System. out. println ("*******" + d1.compareTo (d2); System. out. println (d1.getTime (); System. out. println (d2.getTime (); long diff = d2.getTime ()-d1.getTime (); long hour = diff/(1000*60*60 ); diff = diff % (1000*60*60); long minute = diff/(1000*60); diff = diff % (1000*60); long second = diff/1000; system. out. println ("hour =" + hour + ", minute =" + minute + ", second =" + second); // 2685000} catch (ParseException ex) {// Date d1 = df. parse (tmp_date + t1) is an exception thrown by the parse function, so you can write the exception according to the prompts of the function in programming. printStackTrace (); System. out. println ("data parsing exception:" + ex); // log. warn ("****" + ex);} catch (NullPointerException ex) {ex. printStackTrace (); System. out. println ("null pointer exception:" + ex); // log. warn ("****" + ex);} catch (IndexOutOfBoundsException ex) {ex. printStackTrace (); System. out. println ("array out-of-bounds exception:" + ex); // log. warn ("***" + ex);} catch (RuntimeException ex) {ex. printStackTrace (); System. out. println ("RunTime exception, NullPointerException IndexOutOfBoundsException is its subclass" + ex); // log. warn ("****" + ex);} catch (FileNotFoundException ex) {ex. printStackTrace (); System. out. println ("file not found exception:" + ex); // log. warn ("***" + ex);} catch (IOException ex) {ex. printStackTrace (); System. out. println ("IO read exception, is the parent class of FileNotFoundException" + ex); // log. warn ("***" + ex);} catch (Exception ex) {ex. printStackTrace (); System. out. println ("exception, parent class of the preceding exceptions" + ex); // log. warn ("***" + ex );}}}

(2) In short, because Exception is the parent class or base class of those exceptions! These exceptions are their child classes. If exceptions are placed at the beginning, there is no chance to catch them.

Iii. First Dead Code

(1) cause of Dead Code

Programmers who frequently use the MyEclipse or Eclipse editor to write java code may encounter a yellow line warning: dead code; Generally, programmers will ignore these problems and will not affect the program anyway.

Compile and execute. Yes, this is not a bug. It is just a prompt. For an obsessive programmer, he has no problem with the code at all, including the yellow line warning, dead code is the dead generation.

Code, the cause of the ineffective code prompt, and the solution.

As the name suggests, the dead code, that is, the line you wrote is invalid code, but dispensable. To put it bluntly, it is a line of nonsense. You need to check the processing logic of this line, it may be redundant judgment or other redundant code.

For example:

(2) Case 1: Useless condition judgment means that the condition you determine is always true.

If (true & true ){

System. out. println ("execute OK ");

} Else {

System. out. println ("executefail ");

}

Starting from else, it is useless. Because true & true knows the result when compiling the program, the else part is useless, and the compiler knows the code that will not be executed.

Replace:

Boolean a = true;

Boolean B = true;

If (a & B ){

System. out. println ("execute OK ");

} Else {

System. out. println ("executefail ");

}

This problem will not occur because the compiler is not sure whether a and B are always established during compilation.

(2) Case 2: excessive judgment means that the object you determine is never empty. It is similar to Case 1.

TimeLineEventModel dataModel = new TimeLineEventModel ();

If (dataModel! = Null ){

Execute some operations ....

}

The judgment here is redundant, because you have already added this object, so this object will not be empty. How can you leave it blank for the new object?

The code is still to be continued. There may be other cases of dead code. When the encoding meets, add it! Currently, the deadcode prompt usually appears on the if or other judgment conditions.

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.