Kotlin - Exception Handling - throw keyword
Example-1 : throw Keyword
By Using throw keyword we can handover responsibility of Exception handling to the caller Method
Java -
class Test{
public static void main(String args[]){
throw new ArithmeticException()
}
}
Kotlin :
fun main(args:Array<String>){
throw ArithmeticException()
}
================================================
in Kotlin there is Nothing like Checked Exception : It Means We can throw any number of exception from specific
Example : JAVA
class Test{
public static void main(String args[]){
throw new ArithmeticException();
throw new ArithmeticException();
}
}
In this example we will get Compile time error , in java it is not possible to throw more than one exception explicitly from any method , because there is concepts like Checked Exception.
Now Come to Kotlin - In kotlin there is no such a concept : you can throw any number of exception from any block , but yes behavior is same ... after executing exception line , execution control come out from block
class Test{
var main(args:Array<String>){
throw new ArithmeticException();
throw new ArithmeticException();
}
}
In this kotlin Example we wont get any compile time error. simply exception
line executes and getting exception message at runtime.
==================================================
By Using throw keyword we can handover responsibility of Exception handling to the caller Method
Java -
class Test{
public static void main(String args[]){
throw new ArithmeticException()
}
}
Kotlin :
fun main(args:Array<String>){
throw ArithmeticException()
}
================================================
in Kotlin there is Nothing like Checked Exception : It Means We can throw any number of exception from specific
Example : JAVA
class Test{
public static void main(String args[]){
throw new ArithmeticException();
throw new ArithmeticException();
}
}
In this example we will get Compile time error , in java it is not possible to throw more than one exception explicitly from any method , because there is concepts like Checked Exception.
Now Come to Kotlin - In kotlin there is no such a concept : you can throw any number of exception from any block , but yes behavior is same ... after executing exception line , execution control come out from block
class Test{
var main(args:Array<String>){
throw new ArithmeticException();
throw new ArithmeticException();
}
}
In this kotlin Example we wont get any compile time error. simply exception
line executes and getting exception message at runtime.
==================================================
Comments
Post a Comment