Skip to content

dbc2201/ExceptionHandling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Exception Handling in Java

Definitions

  • An exception is an abnormal condition that occurs at run-time.
    Or we can say that,
  • An exception is a run-time error.
  • Catching an exception in Java and processing it is called Exception Handling.
  • In Java, we have objects for exceptions that describe the error that has occurred in the code at run-time.

What happens when an exception (run-time error) occurs?

  1. When an exception (run-time error) occurs, an object (which contains information about the exception) is created and thrown in the method that has created the error.
  2. Either the method can "handle" the exception itself, or it can pass it to some other method.
  3. At some point, the exception will be caught by a method, and will be processed.

NOTE: Exceptions can be automatically generated by the Java Run-Time Environment, or they can be generated manually by the code that we have written.

Keywords for handling Exceptions in Java

  1. try.
  2. catch.
  3. throw.
  4. throws.
  5. finally.

Hierarchy of Exceptions in Java

exception hierarchy in Java

  • The Throwable class in the java.lang package is the superclass of all exception types in Java. (java.lang.Throwable).
  • The java.lang.Throwable class has two direct known subclasses; namely, the Exception class and the Error class, both in the java.lang package. (java.lang.Exception, java.lang.Error)
  • The java.lang.Exception class describes the "exceptional conditions" that we should handle in our programs.
  • The java.lang.Exception class has a subclass called RuntimeException, also in the java.lang package; it describes the types of exceptions that can be automatically handled.
  • The java.lang.Error class describes about problems that cannot be handled by our programs, these are usually major system failures like OutOfMemoryError or StackOverflowError.

The Exception in the UncaughtException.java file

We see the output as (shown in red on the IntelliJ console)

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at exceptions.UncaughtException.main(UncaughtException.java:14)

Let us understand the above line:

  1. The very first word Exception in Exception in thread ... is telling us that an exception has occurred.
  2. We can identify the type of exception that has occurred by looking at the name of the exception, just see the java.lang.ArithmeticException part. Yes, you guessed it right! IT IS indeed a class in the java.lang package.
  3. After the : the compiler is telling us the reason for the occurrence of this exception, here, it is / by zero.
  4. If you look at the last line, you will find a link to the actual file, method and line where the exception has occurred.

Some common Errors and Exceptions in Java:

Errors

  • java.lang.LinkageError
  • java.lang.ThreadDeath
  • java.lang.ClassFormatError
  • java.lang.NoClassDefFoundError
  • java.lang.VirtualMachineError
  • java.lang.InternalError
  • java.lang.OutOfMemoryError
  • ... and many more

Exceptions

  • java.lang.RuntimeException
  • java.lang.ClassNotFoundException
  • java.lang.ArithmeticException
  • java.lang.IllegalAccessException
  • java.lang.IllegalArgumentException
  • java.util.InputMismatchException
  • java.lang.ClassCastException
  • java.lang.InterruptedException
  • java.lang.ArrayStoreException
  • ... and many more

Flowchart of a try/catch block


Some Built-In Exceptions in Java

  1. ArithmeticException
  2. ArrayIndexOutOfBoundsException
  3. ArrayStoreException
  4. ClassCastException
  5. EnumConstantNotPresentException
  6. IllegalArgumentException
  7. IllegalCallerException
  8. IllegalMonitorStateException
  9. IllegalStateException
  10. IllegalThreadStateException
  11. IndexOutOfBoundsException
  12. LayerInstantiationException
  13. NegativeArraySizeException
  14. NullPointerException
  15. NumberFormatException
  16. SecurityException
  17. StringIndexOutOfBoundsExeption
  18. TypeNotPresentException
  19. UnsupportedOperationException
  20. ClassNotFoundException
  21. CloneNotSupportedException
  22. IllegalAccessException
  23. InstantiationException
  24. InterruptedException
  25. NoSuchFieldException
  26. NoSuchMethodException

Checked Exceptions

  • They are child classes of the java.lang.Exception class.
  • It is required to handle a checked exception using try/catch or to declare them using throws.

Unchecked Exceptions

  • They are also known as runtime exceptions.
  • All the Checked (Runtime) exceptions are subclasses (child classes) of java.lang.RuntimeException class.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages