In this simple example, we are going to see some exceptional cases that can be asked in the Exception interview question in Java. Here we are going to see what happened when exceptions have arrived in a particular method.
In java interview question an interviewer may try to create different test cases which are not easy to understand.
exception in java |
The aim of this example is used to get the Name of the User by using getNameById() and then try to insert that name by using insertName method.
If getNameById() return null then insert is going to fail. If getNameById() return null an inserted then its data lost.
Here is the Java Exception program.
public class ExceptionTestCase { public String getNameById(String name) { name.length(); // creating NullPointerException exception return name; } public int insertName() { getNameById(null);// calling Method System.out.println("Inserting Name"); return 0; } public static void main(String[] args) { ExceptionTestCase expTest = new ExceptionTestCase(); expTest.insertName(); System.out.println("In Main Method"); } }
Consider this above scenario we have an Exception in Java, a class named ExceptionTestCase.
In this example, we have two methods first insertName and the next method is getNameById. The first method is used to get Name bypassing Id, Second is used to insert that name.
This is a basic core Java example that is used to help you to understand the exceptional case in Java and also it may be asked by an interviewer in the core Java exception interview question.
Here we are trying to create an Exception and see what happens in a normal flow of a program. In all Test cases, we are trying to pass the null value for creating an exception in getNameById() method.
Case 1: What happens if we don't use any exception handling in java
public String getNameById(String name) { name.length(); // creating NullPointerException exception return name; } public int insertName() { getNameById(null);// calling Method System.out.println("Inserting Name"); return 0; } public static void main(String[] args) { ExceptionTestCase expTest = new ExceptionTestCase(); expTest.insertName(); System.out.println("In Main Method"); } }
When an exception is thrown by the above method getNameById(), you can see it's not handled anywhere in the method, then main() method throws an exception and terminates the program and prints the exception message with a stack trace in the system console. It is going to simply terminate if any exception occurs.
Output : Exception in thread "main" java.lang.NullPointerException atExceptionTestCase.getNameById(ExceptionTestCase.java:5) at
ExceptionTestCase.insertName(ExceptionTestCase.java:10) at
ExceptionTestCase.main(ExceptionTestCase.java:18)
Case 2: Try and catch block to handle Exception
public class ExceptionTestCase { public String getNameById(String name) { try { name.length(); // creating null pointer exception } catch (Exception e) { e.printStackTrace(); } return name; } public int insertName() { try { getNameById(null);// calling Method System.out.println("Inserting Name"); } catch (Exception e) { e.printStackTrace(); } return 0; } public static void main(String[] args) { ExceptionTestCase expTest = new ExceptionTestCase(); expTest.insertName(); System.out.println("In Main Method"); } }
Output : java.lang.NullPointerException
at ExceptionTestCase.getNameById(ExceptionTestCase.java:6)
at ExceptionTestCase.insertName(ExceptionTestCase.java:15)
at ExceptionTestCase.main(ExceptionTestCase.java:27)
Inserting Name
In Main Method
If we used to try and catch block to handle Exception then it will be going to handle that exception. Now getNameById() method throwing NullPointerException and its also been caught there only.
So even if getNameById() throwing an exception its allow to inserted data which may cause data lost problem in Java.
For e.g. of Java Exception : A user tries to do a select operation if no record found then the user trying to insert null;Case 3 What happened if we used throws Exception in getNameById() method
public class ExceptionTestCase { public String getNameById(String name) throws Exception { name.length(); // creating null pointer exception return name; } public int insertName() throws Exception { getNameById(null);// calling Method System.out.println("Inserting Name"); // some more code return 0; } public static void main(String[] args) throws Exception { ExceptionTestCase expTest = new ExceptionTestCase(); expTest.insertName(); System.out.println("In Main Method"); } }
Output : Exception in thread "main" java.lang.NullPointerException at
ExceptionTestCase.getNameById(ExceptionTestCase.java:5) at
ExceptionTestCase.insertName(ExceptionTestCase.java:11) at
ExceptionTestCase.main(ExceptionTestCase.java:20)
While throws an Exception it only gives you an Exception and when your exception generates its follow-like the abnormal way only. So from this, we understand throws cannot handle the exception is only used to throws an Exception.
Case 4: Throw an Exception directly
public class ExceptionTestCase { public String getNameById(String name) { try { name.length(); // creating null pointer exception } catch (Exception e) { throw e; // Throw exception } return name; } public int insertName() { try { getNameById(null);// calling Method System.out.println("Inserting Name"); } catch (Exception e) { e.printStackTrace(); } return 0; } public static void main(String[] args) throws Exception { ExceptionTestCase expTest = new ExceptionTestCase(); expTest.insertName(); System.out.println("In Main Method"); } }
Output : java.lang.NullPointerException
at ExceptionTestCase.getNameById(ExceptionTestCase.java:6)
at ExceptionTestCase.insertName(ExceptionTestCase.java:15)
at ExceptionTestCase.main(ExceptionTestCase.java:26)
In Main Method
In the Above case, getNameById() is throwing an exception using throw e and try to catch in insertName() method and now no inserting is going to happen here.
This all case is going to ask by an interviewer in Java on exception handling. So be try to execute in your pattern
Hope This Help You Do Comment.
إرسال تعليق