2

I've the following ternary expression:

((!f.exists()) ? (f.createNewFile() ? printFile() : throw new Exception("Error in creating file")) : printFile());

For one, or more, reason that I don't know idea IDE say to me that it isn't a statement. Why?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Tinwor
  • 7,765
  • 6
  • 35
  • 56
  • 5
    Without trying to answer the question, just one observation: Are you sure you want your code to look like this? I would kick myself if I wrote it like this and had to revisit it after a couple of weeks.... – Stephen Jul 23 '15 at 08:57
  • Yes for me is more readable and also I use comments in my code – Tinwor Jul 23 '15 at 08:58
  • What are you trying to achive with the throw? You know that the ternary operator only works with assignments? – SomeJavaGuy Jul 23 '15 at 09:01
  • Can you use `throw` in ternary expression? – Andrew Tobilko Jul 23 '15 at 09:13
  • @KevinEsche the ternary operator does not "only works with assignments". – davmac Jul 23 '15 at 09:14
  • Why use `f.exists()` and then `f.createNewFile()` ? - the latter will return false if the file already exists, there's no need to do a separate check first. – davmac Jul 23 '15 at 09:18

4 Answers4

2

this is not valid, you need to return a value

printFile() : throw new Exception("Error in creating file")

try this one

if(f.exists() || f.createNewFile()) {
  printFile();
}else{
  throw new Exception("Error in creating file");
}
Matteo Rubini
  • 831
  • 5
  • 9
1

It looks like you are using it as a statement and not as an assignment

From what SO says it is not possible to do so

Also from another SO Articel, that says you can´t throw an exception in the ternary statement

I think you need to go back to an if-else clause like this:

if (!f.exists()) {
   try {
      f.createNewFile();
      printFile();
   } catch(Exception e ) {
      System.out.println("Error in creating file");
   }
} else {
   printFile();
}
Community
  • 1
  • 1
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • 2
    The links correctly explain the problem. Just your mention of *assignment* is a bit out of place: the elements of a ternary must be **expressions** (throw is not an expression) and the ternary itself must be used as an **expression** (it's used as a statement in the example). BTW, both Eclipse and javac report a **syntax error** on `throw`. – Stephan Herrmann Jul 23 '15 at 09:37
0

The "COND ? Statement : Statement" construct is an expression. It can not be used as a statement. Without assignment it can be used in situation like resolving condition argument in function call or string concatenation.

Func( (COND ? param1 : param2) );
"Hi"+(con?"Miss":"Mr.")+"";
mohit kumar
  • 179
  • 2
  • 10
0

The statements in the ternary operator need to be non-void. They need to return something.

Example:

  • Consider a case, where count variable gets incremented and I am checking for its value and return true or false above certain threshold.
  • System.out.println((count >10 ? true: false));
  • As compared to, count >10 ? true: false where compiler will complain that this is not a statement.
Nikhil Katre
  • 2,114
  • 23
  • 22