Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tutorial 36 (Runtime vs Checked Exceptions) //easy
- //Two basic types of exceptions in Java:
- //1.Checked Exceptions: (what we have looked at so far in the previous two tutorials)
- //-it forces you to handle it
- //2.Runtime Exceptions
- //-exceptions that you are not forced to handle. also called Unchecked Exceptions. such as division by zero
- public class App {
- public static void main(String[] args) {
- // Arithmetic exception ... (divide by zero)
- int value = 7;
- value = value/0; //this compiles but it still throws an exception: "Exception in thread "main" java.lang.ArithmeticException: / by zero"
- //^this is a Runtime Exception
- String text = null;
- System.out.println(text.length());
- // You can actually handle RuntimeExceptions if you want to;
- // for example, here we handle an ArrayIndexOutOfBoundsException
- String[] texts = { "one", "two", "three" };
- try {
- System.out.println(texts[3]);
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println(e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement