One:throws and trycatch differences
(1) For example, Publicfilewriter (String fileName) throws ioexception{}
I 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 of processing:
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 difference between the two operations, throws just declare the exception, the exception is not processed throws just throws an exception to the class declaration, but does not capture the exception, so that other methods to call it to handle,
or continue to throw, throw to the member function of the previous layer function or class: a Try Catch is an exception that is caught and processed by the code that may have an exception
throws just declare the exception, and the exception is not handled
Of course, try and catch are not to be dealt with.
Like code,
try{
FileWriter fw=new FileWriter ("K.txt");
}
catch (IOException e) {}//catch is also empty, it is not handled AH
Second: 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 read does not throw an exception, only, it is possible to throw the corresponding exception, only write catch, otherwise the unwanted catch eclipse will also error bufferedreader BF = New BufferedReader (file);//May throw exception catch (ParseException ex) {//Date D1 = df.parse (tmp_date + T1); Because of the exception thrown by the parse function, it is possible to write the exception ex.printstacktrace () according to the hints of the function. 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 = "2014-04-01"; try{file file = new file ("D:\\a.txt");//This is not read will not throw an exception, only, it is possible to throw the corresponding exception, only write catch, Otherwise the unwanted catch eclipse will also error bufferedreader BF = new BufferedReader (new FileReader (file));D ate D1 = df.parse (tmp_date + T1);D at E 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);d iff = diff% (1000 *60*60); Long minute = diff/(1000*60);d iff = diff% (1000*60); long second = diff/1000; System.out.println ("hour=" + Hour + ", minute=" + Minute + ", second=" + second);//2685000}catch (ParseException ex) {//Dat e D1 = df.parse (tmp_date + T1) is due to the exception thrown by the parse function, so it is possible to write exceptions Ex.printstacktrace () according to the hints of the function in the program; 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 of FileNotFoundException" + ex);//Log.warn ("* * * *" + ex);} catch (Exception ex) {ex.printstacktrace (); System.out.println("exception, parent of various exceptions above" + ex);//Log.warn ("* * * *" + ex);}}}
(2) in short, because Exception This is the exception of the parent class or the base class! Those anomalies are his subclasses,Exception on the front and there is no chance, it catches all the anomalies.
Three: First Encounter dead Code
(1) Cause of Dead code
frequently Used MyEclipse or Eclipse Editor Authoring Java code for programmers, may often encounter a yellow line warning prompt: Dead Code Generally, programmers encounter these problems will be ignored, anyway, it does not affect the program
compile execution. Yes, this is not a bug, just a hint, for an obsessive-compulsive programmer, he has no problem with the code, including the Yellow line warning to be wiped out, here simply say dead code is dead generation
Code, the reason and the workaround for the no-action hint.
As the name implies, the dead code, that you write the line is invalid code, optional, white is a line of nonsense; This is what you need to look at the processing logic of this line, may be redundant judgment or other redundant code;
As in the following cases:
(2) Situation one: the useless condition judgment, is your judgment this condition forever is true
if (true& true) {
System.out.println ("execute OK");
} else {
System.out.println ("Executefail");
}
It is not useful to start from else. Because True&true knows the result at Yi Time, the else part is useless and the compiler knows the code that is definitely not executing.
Into:
Boolean a =true;
Boolean B = true;
if (A & B) {
System.out.println ("execute OK");
} else {
System.out.println ("Executefail");
}
The problem does not occur because the compiler does not determine whether A & B is constant at compile time.
(2) Situation two: superfluous judgment is that the object you judge is never empty; in fact, it is similar to the situation
Timelineeventmodel Datamodel = new Timelineeventmodel ();
if (Datamodel!=null) {
perform some actions ... .
}
The judgment here is also superfluous, because you have new this object, the object will not be empty, you just new object, how can be empty?
Not to be continued, there may be other cases of death code, wait until the time the code met and then add it! For the time being,deadcode tips generally appear on the if or other judging conditions.
Java exception Real-combat chapter (Trows and try Catch Dead Code)