java-10 Abnormal handling hands-on brain

Source: Internet
Author: User

1. Read and run the Aboutexception.java sample, and then follow the next few pages to learn the basics of implementing exception handling in Java.

Importjavax.swing.*;classaboutexception { Public Static voidMain (string[] a) {intI=1, j=0, K; K=i/J; Try{k= i/j;//causes Division-by-zero Exception//throw new Exception ("hello.exception!");    }        Catch(ArithmeticException e) {System.out.println ("was removed by 0. "+e.getmessage ()); }        Catch(Exception e) {if(Einstanceofarithmeticexception) System.out.println ("Divide by 0"); Else{System.out.println (E.getmessage ()); }    }        finally{Joptionpane.showconfirmdialog (NULL, "OK"); }          }}

Put the code in the TRY statement block where the error may occur. An exception object is thrown when the program detects that an error has occurred. The exception handling code captures and handles this error. The code in the Catch statement block is used to handle errors. When an exception occurs, the program control process jumps from the TRY statement block to the catch statement block. The statements in the finally statement block are always guaranteed to be executed, regardless of whether an exception occurs.

2. Read the following code (Catchwho.java) to write out the results of the program operation:

 Public classcatchwho { Public Static voidMain (string[] args) {Try {                 Try {                     Throw Newarrayindexoutofboundsexception (); }                 Catch(arrayindexoutofboundsexception e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/Inner layer Try-catch"); }             Throw NewArithmeticException (); }         Catch(ArithmeticException e) {System.out.println ("Happened ArithmeticException"); }         Catch(arrayindexoutofboundsexception e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/outer try-catch"); }     } }

Write the results of the Catchwho2.java program run:

 Public classCatchWho2 { Public Static voidMain (string[] args) {Try {                Try {                     Throw Newarrayindexoutofboundsexception (); }                 Catch(ArithmeticException e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/Inner layer Try-catch"); }            Throw NewArithmeticException (); }         Catch(ArithmeticException e) {System.out.println ("Happened ArithmeticException"); }         Catch(arrayindexoutofboundsexception e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/outer try-catch"); }     } }

When there are multiple layers of nested Finally, exceptions are thrown at different levels and thrown at different locations, which can result in a different order of the finally statement block execution.

3. Discrimination: The finally statement block will certainly be executed?

 Public classsystemexitandfinally { Public Static voidMain (string[] args) {Try{System.out.println ("In Main"); Throw NewException ("Exception is thrown in main"); //system.exit (0);                }                Catch(Exception e) {System.out.println (E.getmessage ()); System.exit (0); }                finally{System.out.println ("In finally"); }        }}

So the finally statement does not necessarily perform

4. How do I track the propagation path of an exception?

//Usingexceptions.java//demonstrating the getMessage and Printstacktrace//methods inherited into all exception classes. Public classPrintexceptionstack { Public Static voidMain (String args[]) {Try{method1 (); }      Catch(Exception e) {System.err.println (E.getmessage ()+ "\ n" );      E.printstacktrace (); }   }    Public Static voidMethod1 ()throwsException {method2 (); }    Public Static voidMETHOD2 ()throwsException {method3 (); }    Public Static voidMETHOD3 ()throwsException {Throw NewException ("Exception thrown in Method3" ); }}

When an exception occurs in the program, the JVM looks for the error handler in order of the method invocation.

Each object of the Throwable class has a GetMessage method that returns a string that is passed in the exception constructor and typically contains information about a particular exception.

5. Results:

ImportJava.util.Scanner;classJudgeexceptionextendsexception{ Publicjudgeexception (String s) {Super(s); }}classJudgeException2extendsexception{ PublicJudgeException2 (String s) {Super(s); }} Public classTest { Public Static voidMain (string[] args) {Booleanp=true;  while(P) {System.out.println ("Please enter the student's score:"); Scanner in=NewScanner (system.in); String s=In.next (); Try{             for(intI=0;i<s.length (); i++){                if((S.charat (i) <48| | S.charat (i) >57)){                    Throw NewJudgeexception ("Input error, please enter a number:"); }            }            Try{                intm=Integer.parseint (s); if(m<0| | m>100){                    Throw NewJudgeException2 ("Please enter the correct score"); }                if(m<60) {System.out.println ("Failed"); }                Else if(m>=60&&m<=69) {System.out.println (Failed); }                Else if(m>=70&&m<=79) {System.out.println (In); }                Else if(m>=80&&m<=89) {System.out.println (Good); }                Else if(m>=90&&m<=100) {System.out.println (Excellent); }            }            Catch(JudgeException2 e) {System.out.println (E.getmessage ()); }                         }        Catch(judgeexception e) {System.out.println (E.getmessage ()); }          }    }}

java-10 Abnormal handling hands-on brain

Related Article

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.