简体   繁体   English

未捕获ArrayOutofBound异常

[英]ArrayOutofBound Exception not caught

Hi my code is generating an arrayoutofbound exception which is caught in the code but code terminates abruptly.I want the code to continue. 嗨,我的代码正在生成一个arrayoutofbound异常,该异常被捕获在代码中,但代码突然终止。我希望代码继续执行。 Should an arrayoutofboundexception be caught by Exception or am i doing something wrong, please help catching the error .Exception is expected.Below is the code:- 如果Exception捕获了arrayoutofboundexception或我做错了什么,请帮助捕获错误。期望为Exception。以下是代码:-

try{
    CsgLogin=Ldap.getdomain(requesterLoginId);//This returns domain\usernmae
    LoginIDArray = CsgLogin.split("\\\\");
    requesterLoginId = LoginIDArray[1]; //Exception generated here

} catch(Exception e) {
    System.out.println("Error in the GLDAP lookup or error in Domain Mapping");
    e.printStackTrace();
}

output: 输出:

java.lang.ArrayIndexOutOfBoundsException: 1
        at com.cs.ws.LdapConnect.getdomain(LdapConnect.java:131)
        at com.cs.ws.AgentConnector.startOfBreakGlass(AgentConnector.java:476)
        at com.cs.ws.AgentConnector.runBreakGlassProcess(AgentConnector.java:208)
        at com.cs.ws.MyAccessTimer.main(MyAccessTimer.java:93)
Error in the GLDAP lookup or error in Domain Mapping
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.lang.String.substring(String.java:1768)
        at com.cs.ws.DomainMap.getDomain(DomainMap.java:21)
        at com.cs.ws.AgentConnector.startOfBreakGlass(AgentConnector.java:477)
        at com.cs.ws.AgentConnector.runBreakGlassProcess(AgentConnector.java:208)
        at com.cs.ws.MyAccessTimer.main(MyAccessTimer.java:93)

ArrayIndexOutOfBoundsException is a subclass of Exception and should be caught there. ArrayIndexOutOfBoundsExceptionException的子类,应在此处捕获。 There must be something else going on that isn't shown in your snippet. 片段中肯定没有其他内容。 Please show at least the full method where the issue is stemming from. 请至少显示问题出处的完整方法。 Also, please always include the stacktrace in a question about a thrown exception. 另外,请始终在有关引发异常的问题中包括stacktrace。

The ArrayIndexOutOfBoundsException is being caught: you can tell by the fact that your message Error in the GLDAP lookup or error in Domain Mapping is printed. ArrayIndexOutOfBoundsException 抓住了:你可以通过一个事实,即你的消息告诉Error in the GLDAP lookup or error in Domain Mapping打印。 The first block that you are seeing is just what printStackTrace() produces. 您看到的第一个块就是printStackTrace()产生的。

The two aren't in order because one is printing to the standard output stream and the other is printing to the standard error stream. 两者顺序不对,因为一个正在打印到标准输出流,另一个正在打印到标准错误流。

The exception that is uncaught is the StringIndexOutOfBoundsException that is printed under your message. 未捕获的异常是在您的消息下显示的StringIndexOutOfBoundsException

You're getting the ArrayOutOfBounds Exception when you are trying to access LoginIDArray[1] (ie the 2nd element), because LoginIDArray doesn't have a second element. 当您尝试访问LoginIDArray[1] (即第二个元素)时,您会遇到ArrayOutOfBounds异常,因为LoginIDArray没有第二个元素。

Which means your call to Split("\\\\\\\\") isn't finding any "\\\\" to split by (two escaped backslashes). 这意味着您对Split("\\\\\\\\")调用找不到任何要拆分的"\\\\" (两个转义的反斜杠)。 CSGLogin doesn't contain two backslashes, it has one. CSGLogin不包含两个反斜杠,而是一个。

so, change your Split string to "\\\\" 因此,将您的拆分字符串更改为"\\\\"

ArrayIndexOutOfBoundsException is a subclass of RuntimeException , so it's unchecked - meaning that you don't have to explicitly catch it or declare it. ArrayIndexOutOfBoundsExceptionRuntimeException的子类,因此未选中-意味着您不必显式捕获或声明它。 In a well-written code it shouldn't be necessary to catch it; 在编写良好的代码中,不必捕获它。 surely you have an index error somewhere that's causing the exception. 当然,您在某个地方会出现索引错误,从而导致异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM