How i understand checked and unchecked exceptions. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How i understand checked and unchecked exceptions.

example of unchecked exception- public class Unchecked{ public static void main(String[] args){ int a = 9; int b = 0; System.out.println(a/b); } } this code will complile but will throw an error when running. thats unchecked exception. checked exceptions- example of checked exceptions public class Checked{ public static void main(String[] args){ Thread.sleep(1000); } } this code will not compile unless we surrounded the thread.sleep(1000); with a try catch block. like this- public class Checked{ public static void main(String[] args){ try{ Thread.sleep(1000); } catch(Exception e){ } } } thats checked exception. did i get it wrong plis comment.

31st Mar 2019, 11:16 AM
stephen haokip
stephen haokip - avatar
1 Answer
+ 2
You are correct. Checked exceptions prevent the compilation of the program. With unchecked exceptions the program will still compile but the error will occur at runtime. Additional clarification: https://www.tutorialspoint.com/Checked-vs-Unchecked-exceptions-in-Java
3rd Apr 2019, 1:02 PM
Tibor Santa
Tibor Santa - avatar