Kotlin - println using string template for stderr

How to send output println()

to System.err

. I want to use a string pattern.

val i = 3
println("my number is $i")

      

println()

is sending a message to stdout and it looks like there is no option to send to stderr.

+3


source to share


3 answers


You can simply do it like in Java:

System.err.println("hello stderr")

      



The stdout standard output gets a special shorter version through some helper methods in Kotlin, as this is the most commonly used output. You can use this with the full form System.out.println

.

+3


source


If you want to print an error like Java in Kotlin check the code below:

System.err.println("Printing Error")

      

It will print it in red.



But if you only use println()

then it will work like:

System.out.println("Printing Hello")

      

+1


source


why not create a global function

fun printErr(errorMsg:String){
   System.err.println(errorMsg)
}

      

then call it from any program

printErr("custom error with ${your.custom.error}")

      

+1


source







All Articles