Java 學習記錄81 — Catching and throwing Exception

import java.util.InputMismatchException;
import java.util.Scanner;
public class CatchingAndThrowingExceptions {
public static void main(String[] args) {
int result = divide();
System.out.println("x / y = " + result);
}
private static int divide() {
int x = getInt();
int y = getInt();
System.out.println("x is " + x + ", y is " + y);
return x / y;
}
private static int getInt() {
Scanner s = new Scanner(System.in);
System.out.print("Please enter an integer: ");
while (true) {
try {
return s.nextInt();
} catch (InputMismatchException e) {
// go round again. Read past the end of line in the input first
s.nextLine();
System.out.println("Please enter a number using only the digits 0 to 9 ");
}
}
}
}
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CatchingAndThrowingExceptions {
public static void main(String[] args) {
int result = divide();
System.out.println("x / y = " + result);
}
private static int divide() {
int x;
try {
x = getInt();
} catch (NoSuchElementException e) {
x = getInt();
}
int y = getInt();
System.out.println("x is " + x + ", y is " + y);
return x / y;
}
private static int getInt() {
Scanner s = new Scanner(System.in);
System.out.print("Please enter an integer: ");
while (true) {
try {
return s.nextInt();
} catch (InputMismatchException e) {
// go round again. Read past the end of line in the input first
s.nextLine();
System.out.println("Please enter a number using only the digits 0 to 9 ");
}
}
}
}
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CatchingAndThrowingExceptions {
public static void main(String[] args) {
int result = divide();
System.out.println("x / y = " + result);
}
private static int divide() {
int x, y;
try {
x = getInt();
y = getInt();
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Fuck off! I need an Int");
}
System.out.println("x is " + x + ", y is " + y);
try {
return x / y;
} catch (ArithmeticException e) {
throw new ArithmeticException("Stupid! u can't divide by 0 ");
}
}
private static int getInt() {
Scanner s = new Scanner(System.in);
System.out.print("Please enter an integer: ");
while (true) {
try {
return s.nextInt();
} catch (InputMismatchException e) {
// go round again. Read past the end of line in the input first
s.nextLine();
System.out.println("Please enter a number using only the digits 0 to 9 ");
}
}
}
}
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CatchingAndThrowingExceptions {
public static void main(String[] args) {
try {
int result = divide();
System.out.println("x / y = " + result);
} catch (ArithmeticException e) {
e.printStackTrace();
System.out.println("Unable to perform division, autopilot shutdown.");
}
}
private static int divide() {
int x, y;
try {
x = getInt();
y = getInt();
System.out.println("x is " + x + ", y is " + y);
return x / y;
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Fuck off! I need an Int");
} catch (ArithmeticException e) {
throw new ArithmeticException("Stupid! u can't divide by 0 ");
}
}
private static int getInt() {
Scanner s = new Scanner(System.in);
System.out.print("Please enter an integer: ");
while (true) {
try {
return s.nextInt();
} catch (InputMismatchException e) {
// go round again. Read past the end of line in the input first
s.nextLine();
System.out.println("Please enter a number using only the digits 0 to 9 ");
}
}
}
}
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CatchingAndThrowingExceptions {
public static void main(String[] args) {
try {
int result = divide();
System.out.println(result);
} catch (ArithmeticException | NoSuchElementException e) {
System.out.println(e);
System.out.println("Unable to perform division, autopilot shutting down");
}
}
private static int divide() {
int x, y;
x = getInt();
y = getInt();
System.out.println("x is " + x + ", y is " + y);
return x / y;
}
private static int getInt() {
Scanner s = new Scanner(System.in);
System.out.println("Please enter an integer ");
while (true) {
try {
return s.nextInt();
} catch (InputMismatchException e) {
// go round again. Read past the end of line in the input first
s.nextLine();
System.out.println("Please enter a number using only the digits 0 to 9 ");
}
}
}
}

本篇代碼

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store