- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
让我解释一下我在 java android 中做了什么。
方法 1:
我定义了三个自定义异常。
Public class invalidEmpIDException extends Exception{
public Integer empID;
public MyExceptionONE(Integer id) {
this.empID = id;
}
public Integer getEmpID () {
return this.empID;
}
}
Public class invalidPermissionEception extends Exception{
public Integer empID;
public MyExceptionTWO(Integer id) {
this.empID = id;
}
public Integer getEmpID () {
return this.empID;
}
}
Public class MyExceptionTHREE extends Exception{
public Integer empID;
public MyExceptionONE(Integer id) {
this.empID = id;
}
public Integer getEmpID () {
return this.empID;
}
}
注意:
编写的函数大部分时间都需要针对不同条件抛出所有三个异常。
示例:
public myfunction throws invalidEmpIDException ,invalidPermissionEception, MyExceptionTHREE
{
// My Code here..
// On some condition throws invalidPermissionException,
// on Some condition throws invalidEmpIDException,
// on Some cndition throws MyExceptionTHREE..
}
现在消费者写道:
try {
myfunction
}
catch ( invalidEmpIDException e)
{
}
catch ( invalidPermissionException e)
{
}
catch ( MyExceptionTHREE e)
{
}
最初我觉得这种方法没问题,但当情况出现时用户需要在很多地方调用“myfunction”,那么这会使代码变得笨拙。因此我尝试了以下方法。
catch ( invalidEmpIDException | invalidPermissionException | MyExceptionTHREE ex ) {
ex.getEmpID()
}
问题1:无法解析此 getEmpID ()
从我的搜索中发现,上述问题可以通过引入继承自Exception的BaseClassExcetion,然后引入继承自BaseClassException的所有其他子类来解决
因此,
public Class BaseException extends Exception
{
public Integer empID;
public BaseException(Integer id) {
this.empID = id;
}
public Integer getEmpID () {
return this.empID;
}
}
}
public Class invalidEmpIDException extends BaseException
{
public invalidEmpIDException(Integer id) {
this.empID = id;
}
public Integer getEmpID () {
return this.empID;
}}
public Class invalidPermissionException extends BaseException
{
public invalidPermissionException(Integer id) {
this.empID = id;
}
public Integer getEmpID () {
return this.empID;
}}
}
public Class MyExceptionTHREE extends BaseException
{
public MyExceptionTHREE(Integer id) {
this.empID = id;
}
public Integer getEmpID () {
return this.empID;
}}
}
上述方法解决了我的问题1,但导致了下面解释的另一个问题
问题:
1) 在 invaliePermissionException 子类中定义一个新方法,该方法仅适用于该类。
Example:
public Integer getUserCode() {
}
2)现在我如何在catch block 中使用特定于invalidPermissionException的访问getUserCode?
catch ( invalidEmpIDException | invalidPermissionException | MyExceptionTHREE ex ) {
ex.getEmpID()
}
如果我需要改变我的方法,请告诉我,因为这是我第一次尝试在 java 中处理异常。
最佳答案
使用多重 catch 的方法之所以有效,是因为 ex
将是所有异常都具有的最常见类型。在您的情况下,它将是BasicException
。如果您想对不同的异常执行不同的操作,您应该返回到使用单独的 catch block 的第一种方法,因为您将无法访问自定义异常类中定义的每个特定方法。
作为 Jägermeister 声明,另一种方法可能是使用抽象类,这样您只需要调用 createExceptionMessage
并使您的其他异常创建此方法的实现。:
public abstract class CustomBaseException extends Exception{
private static final long serialVersionUID = 1L;
abstract void createExceptionMessage();
public class Ex1 extends CustomBaseException {
@Override
void createExceptionMessage() {
// TODO Auto-generated method stub
}
}
public class Ex2 extends CustomBaseException {
@Override
void createExceptionMessage() {
// TODO Auto-generated method stub
}
}
}
关于java - 如何在 Java 中管理多个自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412319/
我是一名优秀的程序员,十分优秀!