[英]Learning Try-Catch
我是Java初學者,所以請耐心等待
static int load = 100;
static int greet;
public void loadDeduct(int cLoad, int c){
int balance;
balance = cLoad - 7;
System.out.println("Your balance: " + balance);
}
public void loadDeduct(int tLoad){
int balance;
balance = tLoad - 1;
System.out.println("Your balance is: " + balance);
}
public static void main (String [] args){
int choice;
Scanner scan = new Scanner(System.in);
System.out.println("I'm a cellphone, what do you want to do?");
System.out.println("Press 1 to send SMS / Press 2 to Call");
choice = scan.nextInt();
CellphoneLoad N95 = new CellphoneLoad();
if (choice == 1){
N95.loadDeduct(load);
}else if (choice == 2){
N95.loadDeduct(load, greet);
}else{
System.out.println("Invalid Option!!!");
}
如何使用此程序實現異常處理? 我不太確定如何使用catch塊,因為我們還沒有教過關於整個異常的事情。 這只是我們被要求做的一項運動。 我想用try-catch塊替換if else語句......這可能嗎?
在Java中考慮異常的一個重要原則是有兩種類型:1。運行時2.類型/顯式(缺少更好的單詞)
當出現編程錯誤時,應該拋出運行時異常,並且通常不應該捕獲它們,除非您在頂層捕獲報告錯誤。
類型/顯式異常在方法調用上進行修飾,並且應該在那里,以便調用者可以對它們采取某些操作。
在上面的代碼的情況下,沒有一個地方感覺它應該使用異常處理。
正如Patrick指出的那樣,您通常不希望使用流量控制的異常。
使用Exceptions進行流量控制並不理想。 從您的代碼中不清楚可能會拋出什么異常。 也許你可以再詳細說明一下。
代碼中唯一可能引發異常的部分是調用:
scan.nextInt();
根據JavaDocs,這可能會引發以下可能的異常:
因此,如果您希望您的代碼考慮到拋出這些異常的可能性,您應該像這樣重寫它:
try {
choice = scan.nextInt();
}
catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
catch (NoSuchElementException e) {
System.out.println(e.getMessage());
}
catch (IllegalStateException e) {
System.out.println(e.getMessage());
}
一般來說,你希望你的“捕獲”塊開始具體或很可能發生在不太可能/更一般的性質。
你可以另外“拋出”異常,以便異常發生的任何方法都不處理它 - 調用異常導致方法的方法必須處理它(或者再次拋出異常等,直到它到達Java運行時)。
如果您希望替換“if”語句,我建議使用“switch”語句:
switch (choice) {
case 1: N95.loadDeduct(load);
break;
case 2: N95.loadDeduct(load, greet);
break;
default: System.out.println("Invalid Option!!!");
}
Scanner.nextInt()
方法可以拋出一些例外。 API規范的鏈接頁面列出了可以拋出的三個例外。
例如,如果輸入非整數值,例如"one"
而不是1
,則可以拋出InputMismatchException
。
通常, try-catch
用於捕獲異常,如以下代碼所示:
try
{
Integer.parseInt("one"); // Statement that can cause an exception.
}
catch (NumberFormatException e) // Specify which exception to catch.
{
// Code to handle the NumberFormatException.
}
在這段代碼中添加異常並沒有增加太多價值。 我能想到的是這樣的:
public static void main (String [] args){
.....
try{
handleUserChoice(choice);//new method
}
catch(InvalidChoiceException e){
System.out.println("Invalid Option!!!");
}
}
這真的很簡單。 首先確定你想要的地方。 可以通過庫代碼拋出除外,也可以自己拋出。 然后使用try .. catch來處理它。 這是一個簡單的“你好,世界”的例子:
public class TryTry {
public void doIt() throws Exception {
System.err.println("In the doIt method.");
throw new Exception("Hello there!");
}
public static void main(String[] argv){
TryTry t = new TryTry();
// here we'll catch it.
try {
System.err.println("About to call doIt().");
t.doIt();
} catch (Exception e) { // e now has your exception object
System.err.println("In the exception handler.");
System.err.println("Exception says: "+ e);
}
}
}
throw的作用是構造該異常對象並將其發送到堆棧直到它被捕獲。 為了捕獲它,你可以使用tr { ... }
包圍可能拋出它的代碼,並使用catch
處理異常。
結果如下:
javac TryTry.java && java TryTry About to call doIt(). In the doIt method. In the exception handler. Exception says: java.lang.Exception: Hello there!
我沒有看到任何使用異常而不是if-else塊的理由。 你可以嘗試使用switch語句,它看起來更好。
您應該使用異常來處理方法loadDeduct中可能發生的錯誤。 然后你會用一個try-catch塊來包圍調用N95.loadDeduct的行,寫下如果loadDeduct出錯會發生什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.