Skip to content

Instantly share code, notes, and snippets.

@DaChelimo
Last active April 20, 2021 19:27
Show Gist options
  • Save DaChelimo/4b613db636d5d6986373e1d4261a9b63 to your computer and use it in GitHub Desktop.
Save DaChelimo/4b613db636d5d6986373e1d4261a9b63 to your computer and use it in GitHub Desktop.
Codewars Throw without throwing
import java.util.*
import java.io.*
import java.nio.channels.FileChannel.MapMode
fun arrayIndexOutOfBound() {
val a = arrayOf<Int>()[-2]
}
fun negativeArraySize() {
val n = Array(-10, {0})
}
fun noSuchElement() {
listOf<Int>().iterator().next()
}
fun arithmetic() {
val a = 7 / 0
}
fun classCast() {
val c = 4 as Boolean
}
fun stackOverflow() {
if (true) stackOverflow()
}
fun nullPointer() {
val a: String? = null
a!!
}
fun numberFormat() {
"hka".toInt()
}
fun illegalArgument() {
val a: String? = ""
a?.toInt()
}
fun emptyStack() {
val s = Stack<Int>()
s.pop()
}
fun bufferOverflow() {
val f = File("text.txt")
val raf = RandomAccessFile(f, "rw")
val ch = raf.channel
val buf = ch.map(MapMode.READ_WRITE, 0, f.length())
buf.put(ByteArray(10) { pos -> 1.toByte() })
}
fun arrayStore() {
val stringLists = Array<List<String>>(1) { emptyList() }
Arrays.fill(stringLists,' ')
}
fun unsupportedOperation() {
val newList = Collections.unmodifiableList(listOf<Int>())
newList.add(3)
}
fun illegalState() {
val a = arrayListOf<Int>(1, 2, 3, 4, 5)
a.listIterator().remove()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment