Java Exception Handling

Source: Internet
Author: User

The hierarchy of exceptions
All exception classes are sub-types of the Java.lang.Exception class. Exception classes are subclasses of the Throwable class. Except for the exception class, the Error class is also a subclass of the Throwable class.
1. Public String getMessage ()
Returns details about the occurrence of an exception, which is initialized in the Throwable constructor
2. Public Throwable Getcause ()
Returns the reason that the exception occurred, represented by the Throwable object
3. Public String toString ()
Returns the name of the class associated with the result of GetMessage ()
4. public void Printstacktrace ()
Prints the result of the stack address of the toString () Trace Error output stream
5. Public Stacktraceelement [] Getstacktrace ()
Returns an array that contains the address of each element at the stack, the index of the element 0 represents the top of the call stack, and the last element represents the bottom of the method call stack
6. Public Throwable fillinstacktrace ()
Fills the stack address of the Throwable object with the current stack address, adding to any previous stack address information

1  PackageTest;2 3 ImportJava.util.Scanner;4 5  Public classTest1 {6      Public Static voidMain (string[] args)throwsinsufficientfundsexception{7Scanner input =NewScanner (system.in);8System.out.println ("Enter integers:");9         intNumber1 =input.nextint ();Ten         intNumber2 =input.nextint (); One         //1. A //if (number2! = 0) - //System.out.println (number1 + "/" + Number2 + "is" + (Number1/number2)); - //Else the //System.out.println ("Hello"); -         //2. -         Try{ -System.out.println (Number1 + "/" + Number2 + "is" + (Number1/number2)); +         } -         //Catch keyword Indicates an exception that can be caught +         Catch(Exception e) { ASYSTEM.OUT.PRINTLN ("The program has an exception:" +e); at}finally { -System.out.println ("End"); -         } -         //3. -         if(Number2! = 0){ -System.out.println (Number1 + "/" + Number2 + "is" + (Number1/number2)); in}Else{ -             //Throw Exception to             Throw NewArithmeticException ("wrong")); +              - //ArithmeticException ex = new ArithmeticException ("ok~"); the //System.out.println (Ex.getmessage ()); * //            //throw keyword, called throw an exception $ //throw ex;Panax Notoginseng         } -         //4. Custom Exception Handling the         Try{ +System.out.println ("Start"); A check_test (); the}Catch(insufficientfundsexception e) { +System.out.println (E.getscore () + "-" +e); -}finally{ $System.out.println ("End"); $         } -     } -      Public Static voidCheck_test ()throwsinsufficientfundsexception{ the         DoubleAC = 1.1; -System.out.println ("Check_test ()");Wuyi         Throw Newinsufficientfundsexception (AC); the     } -}

1  PackageTest;2 3  Public classInsufficientfundsexceptionextendsexception{4     5     Private Doublescore;6     /**7 * Custom Exception handling methods8      * @paramscore9      */Ten      PublicInsufficientfundsexception (Doublescore) { One         Super("Hello Superman"); A          This. score =score; -     } -      the      Public DoubleGetscore () { -         returnscore; -     } -}
Insufficientfundsexception.java

Java Exception Handling

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.