Java Basic Learning--exception

Source: Internet
Author: User

To understand by example:

 Packageexception; Public classExceptioncatch {PrivateString s =NULL;  Public Static voidMain (string[] args) {exceptioncatch EC=NewExceptioncatch (); Try{ec.s.tostring (); }Catch(Exception e) {System.out.println ("Caught exception:" +e); }    }}//output:caught exception:java.lang.NullPointerException

1  Packageexception;2 3 //note the differences between the two exception class constructors.

  /**
  * Inference
:
  * The first exception class is defined by itself as a string variable that is passed to the variable at the time of construction;
* While the constructor of the second exception class overrides the constructor of the parent class, we suspect that there must be a member variable of type string in the parent class exception (this member variable also inherits from the MyException subclass)  .
* So when constructing, use super () to fetch the parent class's constructor, and then pass the information to the string member variable in the parent class.  
* and the GetMessage () method is inherited from the parent class, and we infer that the GetMessage () method returns exactly the string variable.  
* The string variable that inherits from the parent class in the first exception class is not assigned a value, so a null is obtained by GetMessage ().  
*/ 
4 classMyExceptionextendsException {5 String msg;6      Publicmyexception (String msg) {7          This. msg =msg;8     }9      Public voidprintmsg () {TenSystem.out.println ("msg =" +msg); One     } A } -  - classMyException2extendsException { the      PublicMyException2 (String s) { -         Super(s); -     } - } +  -  Public classE04_exceptionclass { +      Public Static voidMain (String args[]) { A         Try { at             Throw NewMyException ("myexception message"); -}Catch(MyException e) { - e.printmsg (); -System.out.println ("e.getmessage () =" + E.getmessage ());//the output is null.  -         } -          in         Try { -             Throw NewMyException2 ("MyException2 message"); to}Catch(MyException2 e) { +System.out.println ("e.getmessage () =" +e.getmessage ()); -         } the     } * } $ /*output:msg = myexception messagePanax Notoginseng * E.getmessage () = null - * E.getmessage () = MyException2 message the  */

If an exception is thrown in a method, the throws exception name must be declared after its method. If you do not declare it, you must dispose of the exception in this method through try{}catch{} .

A method can declare multiple exceptions to throw throws Exception 1, exception 2 , but there must be multiple catch to catch different exceptions at a later call to the method. Tip: It's a habit to put all possible anomalies (now and possibly later) in the way they are programmed. Because these exceptions may be encountered later in the function extension of the method, the compiler prompts you to catch the exceptions one by one after declaring them, so you just need to fill in the processing in the catch, which will bring some convenience.

  Here is an example:

 Packageexception;classOpenexceptionextendsThrowable {}classCloseexceptionextendsThrowable {} Public classC { Public Static intOpen () {return-1; }         Public Static voidReadFile ()throwsopenexception,closeexception {if(open () = =-1)Throw Newopenexception (); }         Public Static voidMain (string[] args) {Try{readFile (); } Catch(openexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(closeexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    }

Java Basic Learning--exception

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.