Think in Java notes _chapter12_1_exception Base _ exception handling 3_ custom exceptions and handling of multiple exceptions thrown

Source: Internet
Author: User

  1. Custom exceptions
    Package Cn.seven.shengsiyuan.exception;public class MyException  extends exception{//non-runtime exception, Because just inherited exception not runtimeexceptionpublic myexception () {//TODO auto-generated constructor stub}public myexception ( String message) {super (message);//View Source code discovery keeps calling the super () method of the parent class until Throwable}}

  2. One method throws more than one custom exception for handling

    Package Cn.seven.shengsiyuan.exception;public class Myexceptiontest {public void method (String str) throws MyException, Myexception2{if (NULL==STR) {throw new MyException ("Incoming string cannot be null");} else if ("hehe". Equals (str)) {//This is better, there will be no nullpointerexception, because if Str writes Str=null in front of the case, throw new MyException2 ( "Incoming string cannot be Hello");}} public static void Main (string[] args) {//handling of non-runtime Exceptions 1: I declare an exception, who calls me to deal with this exception, and if you do not handle it, continue to throw Myexceptiontest exceptiontest = new Myexceptiontest (); try {exceptiontest.method ("hehe");} catch (MyException e) {//TODO auto-generated catch BlockSystem.out.println ("myexception"); E.printstacktrace ();} catch (MyException2 e) {System.out.println ("MyException2"); E.printstacktrace ();}}}

  3. A method throws multiple custom exceptions when the catch exception and catch custom exception positions are placed
    Package Cn.seven.shengsiyuan.exception;public class Myexceptiontest {public void method (String str) throws exception {if (null = = str) {throw new MyException ("Incoming string cannot be null");} else if ("hehe". Equals (str)) {//This is better, there will be no nullpointerexception, because if Str writes Str=null in front of the case, throw new MyException2 ( "Incoming string cannot be Hello");}} public static void Main (string[] args) {//handling of non-runtime Exceptions 1: I declare an exception, who calls me to deal with this exception, and if you do not handle it, continue to throw Myexceptiontest exceptiontest = new Myexceptiontest (); try {exceptiontest.method ("hehe");} How to handle non-runtime Exceptions 1: I declare an exception, who calls me to handle this exception, and if not, continue to throw catch (MyException e) {//unreachable catch block for myexception. It is already handled by//the catch block for exception//TODO auto-generated catch blockSystem.out.println ("MyException" ); E.printstacktrace ();} catch (MyException2 e) {System.out.println ("MyException2"); E.printstacktrace ();}  catch (Exception e) {/* * * Method () throws the parent Exception object of MyException and Myexception2, * then the following exception captures the need to write a catch Exception statement block, If the catch Exception statement block * is written at the front of all catch statement blocks, then theSome exceptions will be captured by this block of statements, other methods will be error: Unreachable method, that is, you write this method is impossible to execute * unreachable catch block for MyException2. It is already handled * by the catch block for Exception if the Catch Exception statement block is written at the end of all catch statement blocks, then only the corresponding exception thrown by the method methods is captured, not Executes the catch exceptio statement block * (here refers to the method that throws MyException and Myexception2 's parent exception object) *///TODO auto-generated catch BlockSystem.out.println ("Exception"); E.printstacktrace ();}}}

  4. Encapsulating exceptions for Java APIs with custom exceptions
    1. Non-runtime exceptions, methods in JDK thrown as non-runtime exceptions
    2. Converts an exception in the JDK to a custom exception, catches an exception in the catch block, and then encapsulates the exception before throwing an exception
      1. Because most of the time the API that uses the JDK massively throws the checkedexception, can capture, then carries on its own encapsulation, this kind of practice is very common in actual actual development (most Java books are idealized books,,, Just like in Java core technology, we don't have the same kind of toy case.
        public void Method () throws Myexception{try{system.out.println ("Hello");} catch (Exception e) {e.printstacktrace (); System.out.println ("catch"); throw new MyException ();//This allows you to encapsulate exceptions to the Java API and throw}finally{system.out.println (" Finally ");}}

Think in Java notes _chapter12_1_exception Base _ exception handling 3_ custom exceptions and handling of multiple exceptions thrown

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.