preloader

Exception Handling is the mechanism of handling exception to maintain normal flow of program.

ExceptionHandling

  • COURSES

    04 Month

  • DURATION

    01 Hours

Define different type of Exception and its example?

Checked Exception Unchecked Exception
Checked Exception
directly inherit
the property of throwable class
Unchecked Exception
inherit the Runtime Exception
Occurs at Compile time Occurs generally on run time
throws can handle
checked exception
throws can handle both
Sql exception ,
io exception and
class notfound exception
are part of
checked exception
arrayIndex
OuterbondException, arithmetic
exception
and null pointer exception
are example of it.

Difference between Error and Exception?


Error Exception
Error occurs due
to lack of system resource
Exception occurs
due to code
in program
Error generally occurs
on runtime
Exception generally
occurs on compile
and runtime
Once the error
occurred program will
terminated
Program will
not terminated
if we handle it
will follow
  • Outof
    MemoryError
  • Virtual
    machineError
  • NoClass
    DefError
  • NullPointer
    Exception
  • Array
    IndexOuter
    BondException
  • classNot
    foundException

Difference between ClassNotFound Exception and NoClassDefine Error?


ClassNotFound Exception NoClassDefine Error
ClassNotFound
exception occurred
when path of
exception is not
executed correctly
NoClassDefine
error occurred when
class definition
was present at
compile time but missed
at run time.

what is Try, Catch and Finally block in Exception Handling in java?

Try: try block is used to defined the exception ,it is definition block which must include with catch and finally .only try block will not executed ,either catch or finally block must be present in the code along with try block.

Catch: catch block is used to handled the exception .it must be included with try block and followed by finally block. for handling multiple exceptions, we may have multiple catch block.

Try without catch block is possible but catch without try is not possible.
When multiple catch block is used then unchecked exception will be handled first and then exception class other wise unreachable catch lock exception occurred.

Finally: finally block is structure of block which execute to check whether exception is handled properly or not.it is independent of the execution of try and catch block. Try without catch but with finally will be possible.

Finally, will not execute only in two situations:
  • When system crashed.
  • When system. exit () method is called.

Difference between final, finally and finalize keyword in java?

Final Finally Finalize
Final is a keyword Finally is block Finalize is a method
Final restricts the access of class,
method and variable
Finally will execute in exception handling
to check whether exception is handled properly or not
Finalize is used for garbage collection
Final when used with class that cannot be extended further Finally will not executed when system crashed or system. exit() method called It will execute after system.gc() method called.
Final when used with method, that cannot be overridden
When used with variable that cannot be reinitialized.

Difference between Throw and Throws in Java?

Throw() Throws()
Throw is followed by instance Throws is followed by class
Throw is not used to handle checked exception Throws is used to handle checked exception
Throw is used to call exception explicitly Throws defined the exception
Throw is present inside method Throws are present in method signature
Throw handle only one exception at a time Throws can handle more than one exception at a same time

How to create custom Exception in java?

When we extend exception class then custom checked exception is created whereas when runtime exception class is extended hen custom unchecked exception is created.

Create custom exception :
import java.util.Scanner;
          class IlligalException extends Exception {
              public IlligalException(String s) {
                  super(s);
              }
          }
          public class CustomException {
              public static void validate(int age) throws IlligalException {
                  if (age < 20) {
                      throw new IlligalException("Age is below 19");
                  } else {
                      throw new IlligalException("please ignore");
                  }
              }
              public static void main(String[] args) {
                  Scanner sc = new Scanner(System.in);
                  System.out.println("Enter the age");
                  int age = sc.nextInt();
                  try {
                      validate(age);
                  } catch (Exception e) {
                      System.out.println(e);
                  }
              }
          }

What happen if exception throws by main method ?

When exception I thrown by main () method it will terminate the program at runtime and print exception method at console. If file not founded filenotfoundexception thrown.

Related Course

course thumb

Basic Java

All basic level question for java covered in this section

Visit
course thumb

OOP Concept

Inheritance,Polimorphism,Abstraction and Encapsulation

Visit
course thumb

Multithreading

All multi-threading level questions such as thread ,process,synchronization.

Visit