Kotlin Exception Handling
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
When we run a program invariably at points errors and exception cases occur.
It is the responsibility of the programmer to make sure that it is handled in a proper way rather than just crash the application.
Handling Exceptions
Kotlin provides statements to handle exceptions. Four commonly used statements are:
Try and Catch - Back to top
The concept is simple. In a try block you will write statements that you believe can throw an exception.In catch block you will write the exception handling code.
Exception handling can include but not limited to.
- Logging the Error that occurred for developers to see.
- Raising another Exception after handling it.
- Sending an alert email etc.
fun main(args: Array<String>)
{
try
{
var i: Int = 10;
throw Exception("Throwing Exception!")
}
catch(e: Exception)
{
e.printStackTrace()
}
println("Program Proceeds!")
}
Output:
java.lang.Exception: Throwing Exception!
at Simplest_versionKt.main(Simplest version.kt:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jetbrains.webdemo.executors.JavaExecutor.main(JavaExecutor.java:46)
Program Proceeds!
This will lead to an Exception.
The catch block catches this exception and prints the stack trace. The exception of type Exception is captured in a variable called e and we call the instance method printStackTrace which provides us with detailed information on the exception.
Try, Catch and Finally - Back to top
Sometimes your program will have some valuable resources used in try block. You may want to free up these resources.Examples of such resources include but not limited to:
In such cases finally block helps you to write code that executes always.
So whatever happens in try block, exception or no exception, the statements in finally block will get executed.
fun main(args: Array<String>)
{
try
{
var i: Int = 10;
throw Exception("Throwing Exception!")
}
catch(e: Exception)
{
e.printStackTrace()
}
finally{
println("In Finally Block!")
}
}
Output:
java.lang.Exception: Throwing Exception!
at Simplest_versionKt.main(Simplest version.kt:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jetbrains.webdemo.executors.JavaExecutor.main(JavaExecutor.java:46)
In Finally Block!
Try Finally - Back to top
It is also possible to have a try block with just finally statement.The purpose of such a block is to make sure resources are released properly.
fun main(args: Array<String>)
{
try
{
var i: Int = 10;
throw Exception("Throwing Exception!")
}
finally
{
println("Break the rules!");
}
}
Output:
Break the rules!
Exception in thread "main" java.lang.Exception: Throwing Exception!
at Simplest_versionKt.main(Simplest version.kt:12)