Check for exceptions and non-checked exceptions

Source: Internet
Author: User
Tags stack trace

Check for exceptions and non-checked exceptionsJava code
  1. Public class Exceptiontypetest {
  2. Public void dosomething ()throws arithmeticexception{
  3. System.out.println ();
  4. }
  5. Public static void Main () {
  6. Exceptiontypetest ett = new exceptiontypetest ();
  7. Ett.dosomething ();
  8. }
  9. }
<textarea style= "Display:none" class= "java" name= "code" >public class Exceptiontypetest {public void dosomething ( ) throws arithmeticexception{System.out.println ();} public static void Main () {exceptiontypetest ett = new Exceptiontypet EST (); Ett.dosomething (); }} </textarea>

Question 1: Can the above program compile and pass? and explain why.
Answer: can compile pass. Analysis: According to General commonsense, the definition of the DoSomething method is defined by the ArithmeticException exception, which is called in the Main method. Then you should continue to throw or capture it. However, arithmeticexception exceptions are inherited runtimeexception run-time exceptions. Java inside the exception is divided into two categories: Checkedexception (check exception) and unchecked exception (not checked
exception), for the unchecked exception is also called RuntimeException (run-time exception), for the run-time exception, The Java compiler does not require that you must catch it or must continue to throw it, but for checkedexception (check for exceptions) you must either catch or continue to throw in the method.

Issue 2: The above program changes arithmeticexception to IOException can I compile it? and explain why.
Answer: cannot compile through. Analysis: IOException extends Exception belongs to checked Exception, must be processed, or must be caught or must be thrown

Summary: Exceptions in Java fall into two categories: checked Exception ( Check for exceptions) and unchecked exception (not checked for exceptions), also known as RuntimeException (run-time exception) for unchecked exceptions.
Several ways of handling an unchecked exception (unchecked exception):
1, capturing
2, continuing to throw
3, not handling
Check exceptions (checked exception, In addition to RuntimeException, other exceptions are checked exception):
1, continue to throw, negative methods, can always be thrown into the Java virtual machine to process
2, with Try...catch capture
Note , must be handled for the checked exception, or must be caught or must be thrown

*************************************************************************************************************** *********************************

Exception Handling (Exception)
1. Exception: An abnormal event that occurs during a program's re-operation, which interrupts the normal flow of instructions.
Exceptions occur in the run-time of the program, and problems with compilation are called Syntax errors.

2. Exception handling mechanism:
1) When an exception occurs while the program is running, the JVM automatically creates an exception object of that type. At the same time, the exception object is given to the runtime system. (Throws an exception)
2) when the runtime system accepts an exception object, it will find the appropriate processing method near the code that generates the exception.
3) Exceptions are handled in two ways:
1. Capture and Process: The exception is displayed near the code with Try/catch for processing (unreasonable), the runtime system capture will query the corresponding catch processing block, and then the catch processing block to handle the exception.
2. See if the method in which the exception occurred 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 operation and processes. Declaration by throws (reasonable treatment method)

3. Classification of exceptions:
Java.lang.Throwable
|--error: A serious problem inside the JVM. cannot be recovered. The program personnel do not have to deal.
|--exception exception: a common problem. Through reasonable processing, the program can also return to the normal execution process. Requires the programmer to do the processing.
|--runtimeexception: Also known as non-inspected exception (unchecked exception). This type of exception is a logical problem for programmers. should be held accountable. The Java compiler does not enforce the requirement processing. In other words, this kind of abnormal procedure, can be processed, also can not be processed.
|--non-runtimeexception: Also called an exception (checked exception). Such anomalies are caused by some external accidental factors. The Java compiler enforces the requirement for processing. In other words, the program must handle this type of exception.

4. Common Exceptions:
1) Non-inspected: Nullpointerexception,classcastexception,arrayindexsoutofboundsexception,arithmeticexception (arithmetic exception, except 0 overflow)
2) Inspected: Exception,filenotfoundexception,ioexception,sqlexception.

5. Two ways of handling exceptions:
1). Display is handled with try/catch (unreasonable)
try{
Code snippets that may appear abnormal;
}catch (Exception type 1 variable name) {//Handle the exception of the developing type
The code snippet for handling this type of exception;
E.printstacktrace ();
}catch () {
}[finally{
Code snippet to execute regardless of whether an exception occurred;
Often used to clean up resources
}]
2). Declaration 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
The up declaration is to tell the caller of this method that these exceptions should be handled when using this method.


6. Manually throw an exception: When the program logic does not meet the expectations, you want to abort the execution of the subsequent code.
In the code snippet for a method, you can use the Throw keyword to throw an exception manually.
Note: If you manually throw a checked exception, this method must be processed (the exception should be declared up), or if a non-client exception is thrown manually, it can be handled or not handled.

7. Custom exceptions: When you need some class of exception information related to a particular business.
Inheritance exception can be inherited to define a inspected exception. You can also inherit from RuntimeException or its subclasses to define a non-inspected exception.

8. Exception Summary: A diagram of two processing methods.

9. Exception Practice:
It is important to observe the name and line number of the exception thrown.
Exceptions that are known to be handled should be captured and handled, passing exceptions that do not know how to handle them.
Minimize the volume of the TRY statement block.
for (int i=1;i<=1000;i++) {
try{
...
}catch (...)
} not reasonable
Minimize nesting of try-catch statements.
In the Catch block code snippet, you should print the stack trace information in addition to the exception to facilitate debugging.


Use the throws keyword to declare an exception that the method might throw
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 a non-subject, the caller of the method can handle or not handle it.
If the declared exception that is likely to be thrown is examined, the caller of the method must handle it.

Use the Throw keyword to manually throw an exception,
If a manually thrown exception is a checked exception, it must be processed
There are two ways of handling this:
1. The display is treated with try/catch (unreasonable)
2. Declaration by throws (reasonable treatment method)

Exceptions that are known to be handled should be captured and handled, and exceptions that do not know how to handle are passed.

Reprinted from http://yangshen998.iteye.com/blog/1311682

Check for exceptions and non-checked exceptions

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.