GoConqr has a new look! Let us know what you think.

Flashcards

Exception Handling

Jerry Reeves Jr Information Technology English 15 cards

Cards in this deck (15)

  1. 1Brief us on Exception Handling (or) What is an exception?An exception is an error event that can happen during the execution of a program and disrupts its normal flow.
    Exceptions in Java can arise from different kinds of situations such as wrong data entered by the user, hardware failure, network connection failure, or a database server that is down. The code that specifies what to do in specific exception scenarios is called exception handling.
  2. 2What is the difference between Throws and Throws?throw
    Used inside a method when it is required to throw an Exception logically. Used to throw an exception explicitly. It can throw only one exception at a time.
    Syntax: throw new Exception()
    throws
    Used in the method signature when the method has some statements that can lead to exceptions
    Used to declare multiple exceptions, separated by a comma. When an exception occurs, it matches with the declared ones, and throws an exception automatically
    Syntax: public static void writeToFile() throws Exception {}
  3. 3How to handle class not found exceptions?Java ClassNotFoundException occurs when the application tries to load a class but Classloader is not able to find it in the classpath. It is a checked exception, so it needed to be handled.
    To fix ClassNotFoundException, firstly we must go through the exception stack trace. Then,
    Make sure, you've specified the exact classpath.
    Check for classpath settings and make sure the class it’s present at runtime.
    Verify the requesting Class name is correct.
  4. 4Brief us on the keywords - final, finally & staticfinal is the keyword and access modifier which is used to apply restrictions on a class, method, or variable.
    finally, the block in Java Exception Handling executes important code whether the exception occurs or not.
    static keyword is used to create a class-level variable in java. static variables and methods are part of the class, not the instances of the class.
  5. 5Brief us on ErrorsErrors represent irrecoverable conditions such as Java virtual machine (JVM) running out of memory, memory leaks, stack overflow errors, library incompatibility, infinite recursion, etc.
    Errors are usually beyond the control of the programmer and we should not try to handle errors.
  6. 6What is the difference between final and finally?final is a keyword and it can be used to mark a variable "unchangeable". It is used to apply restrictions on class, method and variable. A final class can't be inherited, the final method can't be overridden and the final variable value can't be changed.
    Finally is a code block. It is used with a try-catch block for handling exceptions. Finally, a code block will be executed whether an exception is handled or not
  7. 7Tell us about the various kinds of exceptions. What are some examples of checked and unchecked exceptions?There are 2 types of exceptions
    Checked Exception(java.lang.Exception) -> This exception forces the programmer to handle it at the compile time itself, until the programmer handles it, the compiler won’t allow it to run. Some of the Checked exceptions are FileNotFoundException, ClassNotFoundException,MethodNotFoundException, SQLException, etc.,
    Unchecked Exceptions(java.lang.RuntimeException) -> These exceptions occur at a run time, it is up to the programmer whether he wants to handle this or not, if he doesn’t handle it, it will lead to abnormal termination. Some of the unchecked exceptions are ArithmeticException, NullPointerException,ArrayIndexOutOfBoundException, etc.,
  8. 8Which block has to be followed after the try block?catch or finally block.
  9. 9Does all the code inside the try block will be executed?Whenever an exception is occurred in the try block, the rest of the code after the exception occurs line will not be executed.
  10. 10Can we have multiple catch blocks with the single try block?yes, we can have multiple catch blocks with the single try block.
    Important Rule: The order of the catch block must be from most specific to most general one i.e. catch for ArithmeticException must come before catching for Exception.
  11. 11When we can go for multiple catch blocks?When our program uses various concepts like an array, file handling, database, etc. at the same time and each of them may throw exceptions due to one reason or another.
    To catch the generic exception we simply use 'Exception' in the catch block parameter and it can catch every exception like ArrayIndexOutOfBound, FileNotFound, etc. But the problem with this is we are not catching the specific exception.
  12. 12How can you handle exceptions in Java?We can handle exceptions using
    try, catch, and finally block
    throw, throws
  13. 13What is the difference between exception and error in Java?Errors happen while an application is running. For instance, an Out of Memory Error occurs in case the JVM runs out of memory. On the other hand, exceptions are mainly caused by the application. For instance, Null Pointer Exception happens when an app tries to get through a null object.
  14. 14What is a custom exception?Java allows us to create our exception based on our needs known as a custom exception or user-defined exception.
  15. 15How do you create custom exceptions in java?To create a custom exception, we have to extend the Exception class.

Information

Description
Exception Handling Interview Questions
Created by
Jerry Reeves Jr
Views
1
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?