Java Exception Handling

Source: Internet
Author: User

I. Hands-on brain

1) Code

Import Javax.swing.*;class aboutexception {public   static void Main (string[] a)    {      double i=-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 ("removed by 0.  ") + e.getmessage ());    }        catch (Exception e)    {        if (e instanceof arithmeticexception)            System.out.println ("divide by 0");        else        {              System.out.println (e.getmessage ());            }    }    Finally     {             joptionpane.showconfirmdialog (null, "OK  " +k);             Joptionpane.showinternalconfirmdialog (null, k);     }  }

}

Two. Using the Java exception handling mechanism

Notice the corresponding relationship between the throw and catch, a throw must have a catch

Three. Hands-on brain

1) Source Code

public class Catchwho {public     static void Main (string[] args) {         try {                 try {                     throw new Arrayindexoutofboun Dsexception (); Array subscript out of bounds                }                 catch (ArrayIndexOutOfBoundsException e) {                        System.out.println (  " ArrayIndexOutOfBoundsException "+  "/inner layer Try-catch ");                 }            throw new ArithmeticException (); Arithmetic exception        }         catch (ArithmeticException e) {//Arithmetic exception            System.out.println ("occurs ArithmeticException");         }         catch (ArrayIndexOutOfBoundsException e) {//array subscript out of bounds           System.out.println (  "ArrayIndexOutOfBoundsException" + "/outer try-catch");         }     } }

3) Results Analysis
Throws two exceptions, processing the first time before processing the second time.

Four. Hands-on brain

1) Source Code

public class CatchWho2 {public     static void Main (string[] args) {         try {                try {                     throw new Arrayindexoutofbou Ndsexception ();                 }                 catch (ArithmeticException e) {                     System.out.println ("arrayindexoutofboundsexception" + "/Inner layer Try-catch");                 }            throw new ArithmeticException ();         }         catch (ArithmeticException e) {             System.out.println ("occurs ArithmeticException");         }         catch (ArrayIndexOutOfBoundsException e) {//array subscript out of bounds            System.out.println ("arrayindexoutofboundsexception" + "/ Outer try-catch ");         }     } }

2)

3) Results Analysis

A throw and catch to perform the next exception

Five. Hands-on brain

1) Source Code

public class Embededfinally {public static void main (String args[]) {int result;             try {System.out.println ("in Level 1");  try {System.out.println ("in Level 2");  result=100/0;                                          Level 2 try {System.out.println ("at Level 3");  result=100/0; Level 3} catch (Exception e) {System                                . OUT.PRINTLN ("Level 3:" + E.getclass (). toString ());                                } finally {System.out.println ("at level 3 finally");  }//result=100/0; Level 2} catch (Exception e) {System.ou          T.println ("Level 2:" + E.getclass (). toString ());              } finally {System.out.println ("at level 2 finally");  }//result = 100/0; Level 1} catch (Exception e) {System.out.println (' Level 1: ' + e.g                Etclass (). toString ());                } finally {System.out.println ("at level 1 finally"); }        }}

2) Results

Six. Hands-on brain

1) source program

public class Systemexitandfinally {public       static void Main (string[] args)    {                try{            System.out.println ( "in Main");            throw new Exception ("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");        }        }}

2) Results

Seven. Hands-on brain

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.
Requires that the program be robust enough that it does not crash regardless of what the user enters.

1) Source Code

Import java.util.*;p Ublic class Prograss {public static void main (string[] args) {Scanner sc=new Scanner (syste        m.in);        String Pro;        System.out.println ("Please enter your score:");            while (true) {try{pro=sc.nextline (); if (Pro.matches ("\\d*")) {throw new inexception ("wrong input! No, it's not numbers!            ");                } else{int Proo=integer.parseint (PRO); System.out.print ("Input correct!                ");                Judge (Proo);            Break        }} catch (Inexception e) {System.out.print ("Please re-enter:");    }} sc.close (); } public static void judge (int pro) {if (pro>=0&&pro<60) System.out.println ("Grade unqualified!        "); else if (pro<70) System.out.println ("Score qualified!        "); else if (pro<80) System.out.println ("in the score!        "); else if (pro<90) System.out.println ("Good grades!        "); else if (pro<=100) System. OUT.PRINTLN ("Excellent performance!        ");    else System.out.println ("Input score does not match");    }}class Inexception extends exception{//public inexception (String msg) {super (MSG); }}

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.