Use of Java Exceptions

Source: Internet
Author: User

Classification of 1.exception

Java divides exceptions into two types, checked exception and unchecked exception (generally referred to as runtimeexception).

Checked exception: is not an exception caused by illegal use of the caller or passing in an incorrect parameter, the runtime encounters such an exception that requires the caller to react according to the situation, after the capture the program can continue to run, the code must show the capture, If you throw such exceptions, you also need to display the join throws keyword after the function name

 public Static void throws exception{    thrownew Exception ("checkexceptionmaker Exception");}

Unchecked exception: during a program run, an exception that is caused by the Caller's improper use can be used at this time, and a Run-time exception is allowed, and when the program encounters this exception, it is terminated, and a function that may throw unchecked exception does not need to specify the throws exception type

 public Static void runtimeexceptionmaker () {     thrownew runtimeexception ("runtimeexceptionmaker Exception "); }
Capture of 2.exception

Checked exception need to display the capture, otherwise compile an error, and unchecked exception is optionally captured, in case the Non-capture will be thrown along the function stack (call function Path) and eventually terminate the Program.

1 public static void main (string[] Argv) {2 3 try {4 Checkedexceptionmaker ();5System.out.println ("Pass Checked exception");6 } catch (Exception E) {7 e.printstacktrace ();8         }9System.out.println ("Program is still running ...");Ten Runtimeexceptionmaker (); one  aSystem.out.println (" program was done"); -  -}

Operation Result:

java.lang.Exception:checkExceptionMaker Exception at    edu.xhyzjiji.cn.exception_demo. Exceptionclassify.checkedexceptionmaker (exceptionclassify.java:) at    Edu.xhyzjiji.cn.exception_ Demo. Exceptionclassify.main (exceptionclassify.java:"main"  Java.lang.RuntimeException:runtimeExceptionMaker exception at    edu.xhyzjiji.cn.exception_demo. Exceptionclassify.runtimeexceptionmaker (exceptionclassify.java:) at    Edu.xhyzjiji.cn.exception_ Demo. Exceptionclassify.main (exceptionclassify.java:) program isstill running ...

Note that the code for line 5,12 is not being executed.

/* When do I use check exception, when do I need unchecked exception? */

Checked exception is the interface caller (hereafter referred to as the Caller) input parameter predictable exception to check, such as the divisor is 0, some necessary parameters are null, open a nonexistent file, such as these problems are introduced by the caller, not the interface source code vulnerability, We can boldly use the checked exception to inform the caller of possible input problems, the caller captures the exceptions in Try-catch way, and directs the program to the normal process based on the exception type.

Unchecked exception may be a problem with the interface itself, resulting in an exception, such as an array out-of-bounds access, because the port number is used to create a TCP client failure, these exceptions may have the following characteristics:
1. The interface source code itself has a problem, 2. The exception to make the subsequent program can not run, this time should terminate the program and let the interface developers to modify the potential problems, re-release interface, this time we can choose unchecked Exception.

/*unchecked exception Need try-catch*/

Sometimes in order to program does not crash, need to unchecked exception also try-catch, which depends on the interface source code must be familiar with, but because of the unchecked exception, the caller can not guide the program into the normal process, So even if Try-catch catches an exception, subsequent calls will continue to throw an exception, try-catch's meaning is not much, and will make our code full of Try-catch blocks of Code.

The gentle approach is that we can choose to try-catch possible unchecked exception, re-try several times or cumulative throws, if not resolved, we throw a runtime exception to end the current Program.

3. Nesting of Exceptions/captures

4. Inherit exception, use code to reduce catch count

5.exception Capture principle

Use of Java 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.