Exception Handling in java

Hi, Friends today topic is Exception handling in Java.

What is an Exception?

  • An exception Unexpected situation in which our your java code fails to execute or run.
  • Exception is a abnormal contentions.
 

Some Basic Exceptions Java codes:

1.Arithmetic Exception
      int  a = 7;
      int  b = 0;
      System.out.print(a/b);
Output: Exception in thread "main" java.lang.ArithmeticException: / by zero
2.Array Index Out Of Bounds Exception
      int a[ ] = { 7, 3, 1};
      System.out.print(a[3]);
Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
3. Null Pointer Exception
      String s = null;
      System.out.print(s.length);
Output: Exception in thread "main" java.lang.NullPointerException
4.StringIndexOutOfBoundsException
      String lang = "Java";
      System.out.print(lang.charAt(5));
Output: java.lang.StringIndexOutOfBoundsException
Exception Handling:
  • The Exception handling in the runtime errors so that normal flow of the program can be maintained.
  • Exceptions is an abnormal condition.
Ex. Let’s say you are an airlines company. Now there might be some rules and regulations for all airlines. For example, having a pilot and staff on plane is pretty mandatory. If you violate this rule, you don’t get to fly. This would be a compile time error. The compiler refuses to compile the code itself when you violate code rules.
Exception Handling Key Words:
1.try
  • Used to specify a block.
  • Pleached the exception code.
  • Must be Followed by catch or finally block.
  • Try block can't be alone declaration.
2.Catch
  • Used to find the exception in try block codes.
  • Declare to below the try block
  • Catch block can't be used alone.
3.Finally
  • Finally block used to execute the important codes of the program.
  • It's executed whether an exception handled or not.
  • Finally block can't be used alone. 
4.Throw
  • throw is used to throw an exception
5.Throws
  • Throws used to declare the specific exception.
  • Throws doesn't throw the exception.
  • It is always used with method signature.

When do you use exception in Java?

The question is not trivial. The question does not specify any references. It is not clear if the post ask for what an exeception is meant to be used for or where Exceptions should be used over traditional error handling. I will assume the readers have a basic understanding of exceptions and compare error handling with exception due to traditional error handling.  

What is Java Nullpoint exception?

NullPointerException is an Exception which Java can throw at runtime. Its name is confusing because it is not possible to create pointers explicitly in Java! But it has been around since the very start of the Java language, and wisely, the Java designers never change anything that would break existing code.  

Example:

Enough with the lame example, in short Exception is an object which you throw in case of an exception. You can throw any subclass of an Exception. Exception extends Throwable. RuntimeException(unchecked) and other checked exceptions extend Exception. You can define your own exceptions by extending either Exception or RuntimeException(to create an unchecked exception). You declare that a method is risky by throws clause. You throw an exception objectusing throw.
public void read() throws IOException{ //Your code throw new IOException(); } To catch an exception, you write a try-catch block. Finally block always executes. try{ //Exception code }catch(IOException e){ e.printStackTrace(); } finally{ //close all resources }
A try block should at least have a catch or a finally block. There can be multiple catch blocks to handle multiple exceptions. Always remember the order of exceptions in catch blocks should be from subclass to superclass. That is if you Exception should be in last catch block, other subclasses should be in first catch blocks. I hope this helps.