Java 什么是非法状态异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20169127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:41:17  来源:igfitidea点击:

What is IllegalStateException?

javaillegalstateexception

提问by Borat Sagddiev

I am trying to use the following Fastload API

我正在尝试使用以下 Fastload API

connection ... etc is perfect.

连接...等是完美的。



I know exactly where it fails

我确切地知道它失败的地方

 ...........
 System.out.println(" Streaming " + dataFile);
 pstmtFld.setAsciiStream(1, dataStream, -1); // This line fails
 System.out.println("check the above line"); // This does not go to console
 ...........

Exception is

例外是

Exception in thread "main" java.lang.IllegalStateException: Sample failed.

Exception in thread "main" java.lang.IllegalStateException: Sample failed.

[ODBC Teradata Driver] Invalid precision: cbColDef value out of range

[ODBC Teradata Driver] Invalid precision: cbColDef value out of range



Here is my table that I am trying to upload. It is a .csvformat and when I open it via notepad it look like this

这是我要上传的表格。这是一种.csv格式,当我通过记事本打开它时,它看起来像这样

1,9,Win
2,9,Winc
3,9,Wi

Why do I get this exception? How can I improve it? As far as I understand the problem is pstmtFld.setAsciiStream(1, dataStream, -1);does not accept the dataset somehow and throw an exception

为什么我会收到此异常?我该如何改进?据我了解,问题是以pstmtFld.setAsciiStream(1, dataStream, -1);某种方式不接受数据集并抛出异常

采纳答案by Jon Skeet

Usually, IllegalStateExceptionis used to indicate that "a method has been invoked at an illegal or inappropriate time." However, this doesn't look like a particularly typical use of it.

通常,IllegalStateException用于表示“在非法或不适当的时间调用了一个方法”。但是,这看起来并不是它的特别典型用法。

The code you've linked to shows that it can be thrown within that codeat line 259 - but only after dumping a SQLExceptionto standard output.

您链接到的代码表明它可以在第 259 行的代码中抛出- 但只有在将 a 转储SQLException到标准输出之后。

We can't tell what's wrong just from that exception - and better code would have used the original SQLExceptionas a "cause" exception (or just let the original exception propagate up the stack) - but you should be able to see more details on standard output. Look at that information, and you should be able to see what caused the exception, and fix it.

我们无法仅从该异常中判断出什么问题 - 更好的代码会将原始SQLException异常用作“原因”异常(或者只是让原始异常沿堆栈向上传播) - 但您应该能够看到有关标准的更多详细信息输出。查看该信息,您应该能够看到导致异常的原因并修复它。

回答by Raman Gupta

Illegal State Exceptionis an Unchecked exception.

非法状态异常是未经检查的异常。

It indicate that method has been invoked at wrong time.

它表明该方法在错误的时间被调用。

example:

例子:

Thread t = new Thread();
t.start();
//
//
t.start();

output:

输出:

Runtime Excpetion: IllegalThreadStateException

We cant start the Thread again, it will throw IllegalStateException.

我们不能再次启动线程,它会抛出 IllegalStateException。

回答by Kundan

package com.concepttimes.java;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IllegalStateExceptionDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List al = new ArrayList();
        al.add("Sachin");
        al.add("Rahul");
        al.add("saurav");
        Iterator itr = al.iterator();  
        while (itr.hasNext()) {           
            itr.remove();
        }
    }
}

IllegalStateException signals that method has been invoked at the wrong time. In this below example, we can see that. remove() method is called at the same time element is being used in while loop.

IllegalStateException 表示方法在错误的时间被调用。在下面这个例子中,我们可以看到。remove() 方法在 while 循环中使用元素的同时被调用。

Please refer to below link for more details. http://www.elitmuszone.com/elitmus/illegalstateexception-in-java/

请参阅以下链接了解更多详情。 http://www.elitmuszone.com/elitmus/illegalstateexception-in-java/

回答by Szymon

public class UserNotFoundException extends Exception {
    public UserNotFoundException(String message) {
        super(message)