Java举一个自定义异常类的使用例子
时间: 2024-06-13 18:04:06 浏览: 7
Java中的自定义异常类可以通过继承Exception或RuntimeException类来实现。下面是一个自定义异常类的使用例子:
```
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
public class Test {
public static void main(String[] args) {
try {
throw new MyException("This is my exception.");
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
}
```
在上面的例子中,我们定义了一个名为MyException的自定义异常类,它继承了Exception类。在Test类中,我们通过throw语句抛出了一个MyException异常,并在catch语句中捕获并输出了异常信息。
自定义异常类的使用可以让我们更好地处理程序中可能出现的异常情况,提高程序的健壮性和可靠性。
相关问题
Java自定义异常类例子
以下是一个Java自定义异常类的例子:
```java
public class MyException extends Exception {
private int errorCode;
public MyException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
}
```
这个例子定义了一个继承自Exception类的自定义异常类MyException。这个类包含一个私有的整型变量errorCode和一个构造函数,可以通过构造函数传入错误信息和错误代码。同时这个类还提供了一个公共的方法getErrorCode,用于获取错误代码。通过自定义异常类,我们可以更加精确地抛出和处理异常,从而提高程序的可读性和可维护性。
java自定义一个异常类并写个列子
好的,下面是一个Java自定义异常类的例子:
```java
public class NegativeNumberException extends Exception {
public NegativeNumberException(String message) {
super(message);
}
}
```
这个例子中,我们自定义了一个NegativeNumberException类,它继承自Exception类。我们在类中定义了一个构造函数,它接受一个字符串参数,用于传递异常信息。
现在我们来看一个使用这个自定义异常类的例子:
```java
public class Calculator {
public static int divide(int dividend, int divisor) throws NegativeNumberException {
if (divisor == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
if (dividend < 0 || divisor < 0) {
throw new NegativeNumberException("Both dividend and divisor must be positive");
}
return dividend / divisor;
}
}
```
在这个例子中,我们定义了一个名为Calculator的类,它有一个名为divide的静态方法。这个方法接受两个整数作为参数,并返回它们的商。如果除数为零,我们将抛出一个新的ArithmeticException异常。如果被除数或除数是负数,我们将抛出一个新的NegativeNumberException异常。
现在我们来测试一下这个方法:
```java
public class Main {
public static void main(String[] args) {
try {
int result = Calculator.divide(10, -2);
System.out.println(result);
} catch (NegativeNumberException e) {
System.out.println(e.getMessage());
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
}
}
```
在这个例子中,我们尝试调用Calculator类的divide方法,并传递10和-2作为参数。这将导致我们抛出一个NegativeNumberException异常,因为我们的自定义异常类要求被除数和除数都必须是正数。我们在try-catch块中捕获这个异常,并输出它的错误信息。
输出结果应该是:
```
Both dividend and divisor must be positive
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)