Download - Exceptions handling notes in JAVA
![Page 1: Exceptions handling notes in JAVA](https://reader036.vdocument.in/reader036/viewer/2022082603/54bdb6ab4a7959070a8b4595/html5/thumbnails/1.jpg)
Exceptions Java handles two types of exceptions. Unchecked exceptions and checked exceptions
Checked and unchecked For unchecked exceptions, programmers need not to handle the exceptions. .i.e. optionalFor checked exceptions programmers have to handle exceptions.
Unchecked exceptions are of two groups. >> Errors >> Runtime exceptions Standard java classes. Inheriting an error class or runt time exception class
Unchecked exception Logical mistakes which can be corrected by the programmer. It doesn’t have to be thrown/ caught by the method.
Example of unchecked exception int div (int a, int b){Return (a/b);}int div() {int num = Integer.ParseInt (args[]);int result = 4 / num;}The above throws Arithmetic Exception, ArrayIndexOutOfBounds Exception and NumberFormatException
Examples For errors,Internal errorNo such method errorNo such field errorIllegal access error
Run time exceptions Null pointer exceptionArray index out of bound exception
Checked Exceptions which are environment or server related and which are beyond the control of the programmers. These exceptions need to be caught by three ways.Connection Refused – Related to DBBindException = Previous running server current session cannot be joined
![Page 2: Exceptions handling notes in JAVA](https://reader036.vdocument.in/reader036/viewer/2022082603/54bdb6ab4a7959070a8b4595/html5/thumbnails/2.jpg)
IllegalStateException = Server not startedException
Exception hierarchy Throwable _|_Error/RTE Exception | |Unchecked Checked
Types of exception handling Try catch Defer using throw or throws
throws is attached next to the method which potentially throws checked exception; We can also actually do the logic and find where exactly the exception is thrown and use the throw keyword which is like return keyword which returns the Exception object.
What is the sequence of catch block?First catch the child exception and then parent exception
Finally block It is used along with try catch. Java executes finally block after try block or catch block
Note – Clean up code is generally written into the finally block.
Sample Code done in class package com.canvas.dec12;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.Properties;
public class LearningErrorHandling {
/** * @param args * @throws Throwable */public static void main(String[] args) throws Throwable {// TODO Auto-generated method stubProperties pr=new Properties();FileInputStream fi=new FileInputStream("c:logg.properties");convertStringToDouble("100.5");
![Page 3: Exceptions handling notes in JAVA](https://reader036.vdocument.in/reader036/viewer/2022082603/54bdb6ab4a7959070a8b4595/html5/thumbnails/3.jpg)
}public static void convertStringToDouble(String s){//try-catchdouble myd=0.0;try{ myd=Double.parseDouble(s); System.out.println("this is the line after converting to double");}catch(Throwable t){System.out.println("this is the catch block");}System.out.println("this is the value of myd----"+myd);}}