Java Applet for exception handling

Source: Internet
Author: User
Tags getmessage

1.Aboutexception Program

Code:

Importjavax.swing.*;classaboutexception { Public Static voidMain (string[] a) {intI=1, J=0, K;//Infinity Positive Infinity//k=i/j;     Try //Monitoring{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"); }   }}

Operation Result:

After modification (delete the statement with the error)

Finally must run.

2. If you change the int type in 1 to double, the output is infinity instead of an exception, because JAVAC generates an INT statement as a idiv bytecode instruction at compile time, and double produces ddiv bytecode instruction, and when the JVM implements these two instructions concretely, Different processing strategies are used, resulting in two code runs with different results.

3. read the code catchwho. Java, write the result of the run

 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"); }     } }

Operation Result:

4. run the Catchwho2 and write the results:

 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"); } //do not allow cumulative errors        Catch(arrayindexoutofboundsexception e) {System.out.println ("ArrayIndexOutOfBoundsException" + "/outer try-catch"); }     } }

Operation Result:

5.embedfinally.java

Code:

 Public classembededfinally { Public Static voidMain (String args[]) {intresult; Try{System.out.println ("In Level 1"); Try{System.out.println ("In Level 2"); //result=100/0; //Level 2  Try{System.out.println ("In Level 3"); Result=100/0;//Level 3}                 Catch(Exception e) {System.out.println ("Level 3:" +E.getclass (). toString ()); }                    finally{System.out.println ("In Level 3 finally"); }               //result=100/0; //Level 2}            Catch(Exception e) {System.out.println ("Level 2:" +E.getclass (). toString ()); }  finally{System.out.println ("In Level 2 finally"); }             //result = 100/0; //Level 1        }        Catch(Exception e) {System.out.println ("Level 1:" +E.getclass (). toString ()); }        finally{System.out.println ("In Level 1 finally"); }  }}

Run:

The finally method must run.

Summary: 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 finally statement block execution.

6. Verify that the finally statement block is bound to execute by Systemexitandfinally.java the sample program.

Code:

 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");} }}

Operation Result:

Thus, thefinally statement block is bound to execute.

7. write a program that requires the user to enter an integer at run time, representing the exam results of a course, followed by a "fail", "Pass", "Medium", "good", "excellent" conclusion.

code ;

 Packageboke1125;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.util.Scanner; Public classGrade { Public Static voidMain (string[] args) { while(true){//judging integers//Judging grade of grade Try{BufferedReader buf=NewBufferedReader (NewInputStreamReader (system.in)); //throw a controlled exceptionSystem.out.println ("Please enter test result: (positive integer)"); intGrade =Integer.parseint (Buf.readline ()); //It is possible to throw a run-time exceptionSystem.out.println ("Score:" +grade); if(grade<0| | grade>100) {MyException1 e1=NewMyException1 ("The score should be between 0~100!") "); ThrowE1; }   if(grade>=0&&grade<60) {System.out.println ("You are in a failed level.");  Break; }   Else if(grade>=60&&grade<70) {System.out.println ("You are at the level of passing.");  Break; }   Else if(grade>=70&&grade<80) {System.out.println ("You're at the middle level.");  Break; }   Else if(grade>=80&&grade<90) {System.out.println ("You're at a good level.");  Break; }   Else if(grade>=90&&grade<=100) {System.out.println ("You're at a good level.");  Break; } }        //The following exception-handling statement blocks are required or cannot be compiled Catch(MyException1 E1) {System.out.println (E1); }      Catch(IOException e) {System.out.println ("I/O error"); }  //The following exception-handling statement block can be omitted without affecting compilation, but error at run time        Catch(NumberFormatException e) {System.out.println ("Score must be an integer"); }}} } classMyException1extendsexception{ PublicMyException1 (String str) {Super(str);}}

Operation Result:

Java Applet for 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.