Original address: http://yangshen998.iteye.com/blog/1311682
[HTML] view plain copy public class Exceptiontypetest {public void dosomething () throws arithmeticexception{ System.out.println (); public static void Main () {exceptiontypetest ett = new Exceptiontypetest (); Ett.dosomething (); } }
Question 1: Can the above program be compiled through. and explain why.
Answer: can compile pass. Analysis: According to General sense, the definition DoSomething method defines the arithmeticexception exception, which is called in the Main method. Then it should continue to be thrown or captured. However, the arithmeticexception exception is an inherited RuntimeException Run-time exception. There are two main types of exceptions in Java: checkedexception (check for exceptions) and unchecked exception (no exceptions), RuntimeException for unchecked exceptions (Run-time exceptions), and for Run-time exceptions. The Java compiler does not require you to capture it or continue to throw it, but the checkedexception (check exception) requires that you must either capture or continue to throw in the method.
Question 2: The above procedure will change arithmeticexception to IOException can compile and pass. and explain why.
Answer: cannot compile through. Analysis: IOException extends Exception belong to checked Exception, must be processed, or must be captured or must be thrown.
Summary: In Java, exceptions fall into two categories: Checked exception (check for exceptions) and unchecked exception (no exceptions are checked) and runtimeexception (Run-time exceptions) for unchecked exceptions.
Several ways to handle unchecked exceptions (unchecked exception):
1. Capture
2, continue to throw
3, do not deal with
There are several ways to handle the exceptions that are checked (checked exception, except RuntimeException, other exceptions are checked exception):
1, continue to throw, negative methods, can always be thrown into the Java virtual machine to deal with
2. Capture with Try...catch
Note that the exception for the check must be handled, or must be caught or must be thrown
1. Exception: An abnormal event that occurs during a program's re-run, which interrupts the normal flow of instructions.
Exceptions occur during the runtime of a program, and the problem of compilation is called a syntax error.
1 The JVM automatically creates an exception object of that type when an exception occurs during the program's re-run. At the same time, the exception object is given to the runtime system. (Throws an exception)
2 when the runtime system receives an exception object, it looks for the appropriate processing near the code that generated the exception.
3 The exception can be handled in two ways:
1. Capture and processing: explicitly handled with Try/catch near the exception's code (unreasonable), the runtime system captures the corresponding catch-handling block and then processes the exception in a catch-handling block.
2. See if the method that has an exception has an upward declaration exception, an upward declaration, a query processing statement to the superior, and if there is no upward declaration, the JVM interrupts the program's running and processing. To declare (a reasonable method of handling) by throws
Java.lang.Throwable
| | | Error ERROR: A serious problem within the JVM. Unable to recover. Program personnel do not have to deal with.
|--exception exception: a common problem. Through reasonable processing, the program can also return to the normal execution process. Programmers are required to process.
|--runtimeexception: Also known as non-inspected anomalies (unchecked exception). This type of exception is a logical problem for programmers. should be held responsible. The Java compiler does not enforce requests for processing. In other words, such exceptions can be processed or not handled in a program.
| |--runtimeexception: Also called the abnormal (checked exception). This kind of anomaly is caused by some external accidental factors. The Java compiler enforces requests for processing. In other words, the program must handle such exceptions.
1 Non-inspected: Nullpointerexception,classcastexception,arrayindexsoutofboundsexception,arithmeticexception (arithmetic exception, except 0 overflow)
2) Inspected: Exception,filenotfoundexception,ioexception,sqlexception.
5. Two ways to handle exceptions:
1). Display to be treated with try/catch (unreasonable)
[HTML] view plain copy try{//code snippet that may be abnormal,}catch (Exception type 1 variable name) {//Handle exception of the specified type//handling code snippet for the type exception; e.printstackt Race (); }catch () {}[finally{//code snippet to execute regardless of whether an exception occurred;//frequently used to clean up resources}]
2. Statement up: Use throws keyword, outward declaration (reasonable processing method)
Declares a list of exceptions that this method might throw.
... Method Name (argument list) throws Exception type 1, exception type 2
An upward declaration is a call to the caller of this method, and these exceptions should be processed when using this method.
6. Manually throw an exception: When the program logic does not meet expectations, to abort the execution of the following code.
In the code snippet of a method, you can manually throw an exception using the Throw keyword.
Note: If you manually throw an exception, the method must be handled (the exception should be declared up), or it can be handled or not handled if the manually thrown exception is a non-client.
You can inherit exception to define a client exception. You can also inherit from RuntimeException or its subclasses to define a non-client exception.
8. Exception Summary: A diagram of two ways of processing.
It is important to observe the name and line number of the thrown exception.
You should capture and handle exceptions that are known to handle, passing exceptions that you don't know how to handle.
Minimize the volume of a try statement block.
[HTML] view plain copy for (int i=1;i<=1000;i++) {try{...} catch (...) Unreasonable to minimize nesting of try-catch statements
In a catch block code snippet, you should print the stack trace information for the exception to facilitate debugging.
Throws can declare that a method may throw one or more exceptions, separated by ', ' between exceptions
If the declared exception that may be thrown is non-inspected, the caller of the method can or cannot handle it.
If the declared exception that might be thrown is a client, the caller of the method must handle it.
If the manually thrown exception is a client exception, then the processing must be done
There are two ways of handling:
1. The display is handled with try/catch (unreasonable)
2. Throws outward declaration (reasonable method of handling)
*************************************************************************************************************** **
Exception Handling (Exception)
2. Handling Mechanisms for exceptions:
3. Classification of anomalies:
4. Common anomalies:
7. Custom exceptions: When you need some exception information class related to a particular business.
9. Unusual Practice:
Use the throws keyword to declare an exception that a method might throw
Use the Throw keyword to manually throw an exception,
You should catch and handle exceptions that are known to handle, and pass exceptions that you don't know how to handle.