48

I am trying to use the following 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.

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


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

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

Borat Sagddiev
  • 807
  • 5
  • 14
  • 28
  • 2
    I usually throw an `IllegalStateException` whenever I catch something that "cannot possibly happen" and there is no other more-descriptive exception type that I can throw. For example, say I write code to display an image, and the image has a negative width - throwing anything else would be worse, so I can either write my own unchecked exception (recommended practice, but then you may consider making it a checked exception instead), or use the built-in `IllegalStateException` one. – tucuxi Nov 24 '13 at 00:03
  • According to http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setAsciiStream%28int,%20java.io.InputStream,%20int%29, is it possible that the file length (-1) is causing this? – ioreskovic Nov 24 '13 at 00:05

3 Answers3

52

Usually, IllegalStateException is 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.

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

We can't tell what's wrong just from that exception - and better code would have used the original SQLException as 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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • what is a `standard output`? – Borat Sagddiev Nov 23 '13 at 23:26
  • 1
    @BoratSagddiev: As in `System.out` - the standard output stream from your process, usually writing to a console if there's one attached, or often captured as a log file in a server-side process. As we don't know how you're running the code, it's hard to tell you where to look. – Jon Skeet Nov 23 '13 at 23:29
  • I have edited the question. I know exactly where it fails. `pstmtFld.setAsciiStream(1, dataStream, -1);` does not execute – Borat Sagddiev Nov 23 '13 at 23:38
  • I don't think it is dumping an SQLexception simply because it fails earlier – Borat Sagddiev Nov 23 '13 at 23:39
  • @BoratSagddiev: That's where it *initially* throws a SQLException - which is caught lower down, dumped, and then an IllegalStateException is thrown. So you really *should* have the SQLException logged. – Jon Skeet Nov 24 '13 at 08:15
  • 1
    @JonSkeet - Is this a good or bad example of using IllegalStateException https://stackoverflow.com/a/42011744/3184475 ? Please advise. Thanks. Btw, I am not the OP. He is a just an imitator. I am the real borat. – Erran Morad Nov 11 '19 at 21:25
  • 1
    @BoratSagdiyev: Fairly reasonable IMO. – Jon Skeet Nov 11 '19 at 22:30
21

IllegalStateException signals that method has been invoked at the wrong time. In the example below we can see that the remove() method is mutating an element while its iterator is in a loop (an illegal call), which Java catches and throws:

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();
        }
    }
}

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

oligofren
  • 20,744
  • 16
  • 93
  • 180
Kundan
  • 219
  • 2
  • 5
  • 17
    Nick You are wrong ! The above answer actually put it me in the right direction to solve similar problem in my code. You should not down vote answers only because they refer to old questions. This is most inappropriate and discouraging to new SO users. – Mario S Oct 12 '18 at 02:16
  • This _was_ actually a great example of an appropriate us of `IllegalStateException`. I had to read it a bit closer to see that. @Nick needs to up his reading comprehension. – oligofren May 31 '21 at 08:33
19

Illegal State Exception is 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.

Raman Gupta
  • 1,580
  • 15
  • 12