EXCEPTION HANDLING Flashcards
What is exception handling?
Java Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
What is an exception?
Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions.
What is the difference between an error and an exception
Error: An Error indicates a serious problem that a reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.
Describe and illustrate the exception hierarchy
All exception and error types are subclasses of class Throwable, which is the base class of the hierarchy. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception. Another branch, Error is used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). StackOverflowError is an example of such an error.
*See https://www.geeksforgeeks.org/exceptions-in-java/#:~:text=Java%20Exception%20Handling%20is%20a,flow%20of%20the%20program’s%20instructions. for pic
What are the different types of exceptions
- Built-in Exceptions
- Checked Exception- Checked exceptions are called compile-time exceptions because these exceptions are checked at compile-time by the compiler. ClassNotFoundException, IOException, SQLException etc are the examples of the checked exceptions.
- Unchecked Exception- The unchecked exceptions are just opposite to the checked exceptions. The compiler will not check these exceptions at compile time. In simple words, if a program throws an unchecked exception, and even if we didn’t handle or declare it, the program would not give a compilation error. Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.
2.User-Defined Exceptions
What is the syntax of the try catch block
try { block of code that can throw exceptions } catch (Exception ex) { Exception handler }
What is the syntax of the try catch finally block
try { block of code that can throw exceptions } catch (ExceptionType1 ex1) { exception handler for ExceptionType1 } catch (ExceptionType2 ex2) { Exception handler for ExceptionType2 } finally { finally block always executes }
Give an example of how to use throw keyword
throw new Exception(“Exception message”);
Give an example of how to use throws keyword
void testMethod() throws ArithmeticException, ArrayIndexOutOfBoundsException { rest of code }