Summary of Java exception content

Source: Internet
Author: User
Tags getmessage throw exception

In the development of the program, there may be various errors, some errors can be avoided, and some errors are unexpected, in Java, these possible errors are referred to as exceptions.

The Throwable class is a superclass of all exception classes, and the two direct subclasses of the class are error and exception. where error and its subclasses are used to indicate that a reasonable application should not attempt to capture a serious problem, exception and its subclasses give reasonable exceptions that the application needs to catch.

Handling Exceptions:

1. Handling Exceptions using Try....catch

        Try {A            statement that requires normal        execution        }catch(Exception ex) {            statement to process the exception        }

Try and catch are keywords for exception handling.

The curly braces in the back of the try are statements that the program needs to execute normally but that may occur.

The exception type that the program needs to handle is within the parentheses of the catch.

A catch's curly braces are statements that process exceptions that occur in a program.

2. Handling Exceptions using try...catch...finally

Try {A            statement that requires normal        execution        }catch(Exception ex) {            statement to process the exception        }        finally  {            The statement that is bound to be processed        }

Try and catch are keywords for exception handling.

The curly braces in the back of the try are statements that the program needs to execute normally but that may occur.

The exception type that the program needs to handle is within the parentheses of the catch.

A catch's curly braces are statements that process exceptions that occur in a program.

Finally, the statement is executed regardless of whether the program has an exception, that is, the program executes the TRY statement or executes the catch statement to execute the FINALLY statement, so the finally statement block is typically used to perform operations such as garbage collection, freeing resources, and so on.

3. Handling Exceptions using try...finally

Try {            A statement that requires normal        execution        }finally{            A statement that is bound to be processed        }

Try and finally are keywords for exception handling.

The two curly braces between try and finally are statements that the program needs to execute normally but that may occur.

Finally, the statements inside the two curly braces are statements that are ultimately executed regardless of whether the program has an exception. Therefore, the finally statement block is typically used to place code that must be executed in the program.

Throw exception:

For exceptions that occur in a program, you can use throws to declare a throw statement to throw an exception, in addition to using TRY...CATCH statement block processing.

The format is as follows:

     Public Static Double throws exception{}

Here's a chestnut:

 Public Static voidMain (string[] args)throwsException {System.out.println ("Please enter a number"); ints = 0; Try{Scanner sc=NewScanner (system.in); String I=Sc.nextline (); S=Integer.parseint (i); }        Catch(Exception e) {System.out.println (E.getmessage ()); Exception ex=NewException (); Throw NewException (ex); }        DoubleA =0; Try{a= 100/s;        System.out.println (a); }        Catch(Exception e) {System.out.println (E.getmessage ()); }            }

The output is:

"ASDASD""main" java.lang.Exception:java.lang.Exception at    com.yichang.Test.main (Test.java: caused by:java.lang.Exception at    com.yichang.Test.main (Test.java:42)

Methods with throws Exception:
1. Exceptions can be avoided try...catch
2. The code for this method must be Try...catch (and can be thrown again, of course).

One more example:

 Public Static DoubleTest ()throwsexception{System.out.println ("Please enter a number"); DoubleA =0; Try{Scanner sc=NewScanner (system.in); String I=Sc.nextline (); ints =Integer.parseint (i); A=100/s; }        Catch(Exception e) {Throw Newmyexception (); }        returnA; }     Public Static voidMain (string[] args)throwsexception{Try {            DoubleD =Test ();        System.out.println (d); } Catch(MyException e) {//TODO Auto-generated catch blockSystem.out.println (E.get ()); }            }

 Public class extends Exception {    public  String get () {        return ' Hahahahahhahahaha ' ;    }}

When the output error is:

Please enter a number Asdasdhahahahahhahahaha

Summary of Java exception content

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.