Download Exceptions in Java and more Slides Java Programming in PDF only on Docsity!
The Exception
Class In Java
Spotle.ai Study Material Spotle.ai/Learn
Spotle.ai Study Material
Not
Everything In
Life Goes
According To
The Plan
Spotle.ai Study Material
An exception in Java denotes an exceptional event occurring during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program terminates abnormally. Exception In Java:
Spotle.ai Study Material
- An invalid input like inputting a divisor as 0.
- (^) A file that the program attempts to open cannot be found.
- (^) The JVM runs out of memory
- A network connection is lost while running a client-server application. Exceptions can occur due to programming error, user error or a problem with the environment during execution:
Spotle.ai Study Material
Checked Exception
- Are exceptions that the compiler warns/ notifies you about.
- FileNotFoundException, IOException, SQLException, ClassNotFoundException InvocationTargetException are types of checked exceptions.
- For example, when you use the FileInputStream class in your program to read from a file, without declaring or catching the exception, then a complilation error occurs as follows: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unhandled exception type FileNotFoundException.
- This exception cannot be ignored and has to be handled by declaring/ catching the exception. Unchecked Exception
- Unchecked exceptions are the exceptions that are not checked at compiled time. Unchecked exceptions are of type Runtime Exceptions.
- The compiler does not warn/ notify you if your program does not declare or catch an unchecked exception.
- They are caused by bad input at execution time.
- All Unchecked exceptions are direct sub classes of RuntimeException class. Example: ArithmeticException, ArrayIndexOutOfBoundsException. For example if you try to divide a number by 0, the ArithmeticException occurs. Error - java.lang.Error class represents the errors which are mainly caused by the environment in which the program executes. For example, OutOfMemoryError occur s when JVM runs out of memory or StackOverflowError occurs when stack overflows. - Errors cannot be declared or caught in the program. Even if you handle them using try-catch blocks, your application will not recover if an error occurs. Unlike in the case of an exception, the normal flow of the program can be resumed if you handle the exception properly.
- The error class is also derived from the throwable class as Exception is derived. Types Of Exception In Java
Spotle.ai Study Material
FAQ: What Is The Throwable Class? The Throwable class is the superclass of all errors and exceptions in the Java language. Any exception or error thrown by a Java program is an instance of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. The throwable class contains methods that return detailed cause, message and stackTrace associated with the exception.
Spotle.ai Study Material
Next Lecture:
How does
Java handle
exceptions?