練習(xí)1
邗江網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
寫一個(gè)方法void triangle(int a,int b,int c),判斷三個(gè)參數(shù)是否能構(gòu)成一個(gè)三角形。如果不能則拋出異常IllegalArgumentException,顯示異常信息:a,b,c “不能構(gòu)成三角形”;如果可以構(gòu)成則顯示三角形三個(gè)邊長。在主方法中得到命令行輸入的三個(gè)整數(shù),調(diào)用此方法,并捕獲異常。
兩邊之和大于第三邊:a+b>c
兩邊之差小于第三邊:c-a
package 異常;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestTriangle {
public static void triangle(int a, int b,int c) throws IllegalArgumentException, InputMismatchException{
int x[] = new int[3];
x[0] = a;
x[1] = b;
x[2] = c;
Arrays.sort(x);
if ((x[0]+x[1]>x[2])&&(x[2]-x[1]<x[0]))
System.out.println("三角形的三邊長為:"+a+","+b+","+c);
else
throw new IllegalArgumentException();
}
public static void main(String[] args) {
int a=0, b=0, c=0;
Scanner in = new Scanner(System.in);
System.out.println("請(qǐng)分別輸入三角形的三邊長:");
try{
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
triangle(a, b, c);
}catch(InputMismatchException e1){
System.err.println("請(qǐng)輸入整數(shù)作為三角形的邊長!");
e1.printStackTrace();
}catch(IllegalArgumentException e2){
System.err.println(a+","+b+","+c+"不能構(gòu)成三角形");
}
}
}

練習(xí)2:
從命令行輸入5個(gè)整數(shù),放入一整型數(shù)組,然后打印輸出。要求:
如果輸入數(shù)據(jù)不為整數(shù),要捕獲輸入不匹配異常,顯示“請(qǐng)輸入整數(shù)”;如果輸入數(shù)據(jù)多余5個(gè),捕獲數(shù)組越界異常,顯示“請(qǐng)輸入5個(gè)整數(shù)”。
無論是否發(fā)生異常,都輸出“感謝使用本程序!”
package 異常;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestArray {
public static void main(String[] args) {
int a[] = new int[5];
System.out.println("請(qǐng)輸入5個(gè)數(shù):");
System.out.println("最后輸入一個(gè)非數(shù)字結(jié)束輸入操作。");
Scanner in = new Scanner(System.in);
try{
int i = 0;
while(in.hasNextDouble()){
a[i] = in.nextInt();
i++;
}
if(i<5)
throw new ArrayIndexOutOfBoundsException();
for(int j=0;j<5;j++)
System.out.print(a[j]+" ");
System.out.println();
}catch(InputMismatchException e1){
System.err.println("請(qǐng)輸入整數(shù)作為數(shù)組元素!");
e1.printStackTrace();
}catch(ArrayIndexOutOfBoundsException e2){
System.err.println("請(qǐng)輸入5個(gè)數(shù)!");
e2.printStackTrace();
}finally{
System.out.print("感謝使用本程序!");
}
}
}
總結(jié)
以上就是本文關(guān)于Java編程異常簡單代碼示例的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Java異常退出條件的判斷示例代碼、Java編程異常處理最佳實(shí)踐【推薦】、Java編程中的檢查型異常與非檢查型異常分析等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感謝朋友們對(duì)本站的支持!
網(wǎng)站題目:Java編程異常簡單代碼示例
轉(zhuǎn)載來于:http://www.chinadenli.net/article28/iphdjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)公司、外貿(mào)建站、企業(yè)網(wǎng)站制作、網(wǎng)站排名、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)