Simple Java: 10 FAQ about Java exceptions. simplejava
This article summarizes ten frequently asked JAVA exceptions;
1. Check type exception VS non-Check type exception
To put it simply, a checkpoint exception means that the caller needs to capture the exception or declare that an exception is thrown in the method;
Non-checked exceptions are those that cannot be solved, such as Division 0 and null pointers;
Checktype exceptions are very important because you want the people using your API to know how to handle these exceptions;
For example, IOException is a common check exception. RuntimeException is a non-check exception;
The exception Hierarchy Diagram of Java is as follows:
2. Best practices for exception management
If an exception can be properly processed, capture and process it; otherwise, an exception should be thrown;
3. Why variables defined in the try code block cannot be accessed in the catch or finally code block?
The following Code declares the s variable in the try code block, but cannot be used in the catch code block. The Code cannot be compiled;
try { File file = new File("path"); FileInputStream fis = new FileInputStream(file); String s = "inside"; } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println(s); }
The reason is that you do not know which statement in the try code block will throw an exception. It is very likely that this exception will be thrown before the variable Declaration;
This is exactly the case in the above example;
Note: It is strongly suspected that the s variable is invisible due to the relationship between scopes;
4. Why does Integer. parseInt (null) and Double. parseDouble (null) Throw different exceptions?
This is a problem with JDK, because these two methods are written by Different developers, so there is no need to go into details;
Integer.parseInt(null); // throws java.lang.NumberFormatException: null Double.parseDouble(null); // throws java.lang.NullPointerException
Note: In fact, my current JDK 7 has no such problem. Both of them throw NumberFormatException.
5. Exception during multi-purpose Running
Several common runtime exceptions, such as IllegalArgumentException and ArrayIndexOutOfBoundsException;
When the judgment conditions are not met, you can throw these exceptions, as shown in the following code:
if (obj == null) { throw new IllegalArgumentException("obj can not be null");6. Can I catch multiple exceptions in a catch code block?
The answer is yes. Because JAVA exceptions can be traced back to the same parent class Exception, we can use the parent class to catch multiple exceptions;
try { } catch (Exception e) { // TODO: handle exception }
Supplement:
In Java 7, a new syntax is added to capture multiple exceptions as follows:
try { ...} catch( IOException | SQLException ex ) { ...}
Before Java 7, you can write as follows:
try { //.....} catch (Exception exc) { if (exc instanceof IllegalArgumentException || exc instanceof SecurityException || exc instanceof IllegalAccessException || exc instanceof NoSuchFieldException ) { someCode(); } else if (exc instanceof RuntimeException) { throw (RuntimeException) exc; } else { throw new RuntimeException(exc); }}7. Can the constructor throw an exception?
The answer is yes. The constructor is a special method and can throw an exception;
Note: If the constructor throws an exception, the object is not generated;
8. finally statement blocks can also throw and catch exceptions.
The following code is valid:
public static void main(String[] args) { File file1 = new File("path1"); File file2 = new File("path2"); try { FileInputStream fis = new FileInputStream(file1); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { FileInputStream fis = new FileInputStream(file2); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
But for readability, it is best to encapsulate the exception handling code in finally into a method and then call this method, as shown in the following code:
public static void main(String[] args) { File file1 = new File("path1"); File file2 = new File("path2"); try { FileInputStream fis = new FileInputStream(file1); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { methodThrowException(); } }9. Can I write a return statement in the finally code block?
Yes, you can.
10. Why are JAVA programmers often quietly ignoring exception handling?
The following code snippets often appear in programs. If exception handling is so important, why are there so many developers doing this all the time?
try { ... } catch (Exception e) { e.printStackTrace(); }
The code that appears frequently does not mean it is correct;
Many developers may use eclipse to quickly fix and automatically generate Exception Handling Code. As shown above, there is actually nothing to do except record the log;
As described in entry 2, if the exception cannot be correctly handled, it is best to throw an exception so that the exception can be discovered as soon as possible;
Http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/.