Exceptions In Java.docx

  • Uploaded by: Jitender
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Exceptions In Java.docx as PDF for free.

More details

  • Words: 643
  • Pages: 2
Exceptions in Java Introduction An unwanted event that disturb normal flow of the program is called exception. It is highly recommended to handle exception and the main objective of exception handling is graceful termination of the program.

Runtime stack mechanism 1. For every thread JVM will create a runtime stack each and every call perform by that thread will be stored in the corresponding stack. 2. Each entry in the stack is called stack frame or activation record. 3. After completing every method call the corresponding entry from the stack will be removed. 4. After completing all method calls the stack will be empty and then empty stack will be destroyed by JVM just before terminating the thread.

What are important methods of Java Exception Class? Exception and all of it’s subclasses doesn’t provide any specific methods and all of the methods are defined in the base class Throwable. 1. String getMessage() – This method returns the message String of Throwable and the message can be provided while creating the exception through it’s constructor. 2. String getLocalizedMessage() – This method is provided so that subclasses can override it to provide locale specific message to the calling program. Throwable class implementation of this method simply use getMessage() method to return the exception message. 3. synchronized Throwable getCause() – This method returns the cause of the exception or null if the cause is unknown. 4. String toString() – This method returns the information about Throwable in String format, the returned String contains the name of Throwable class and localized message. 5. void printStackTrace() – This method prints the stack trace information to the standard error stream, this method is overloaded and we can pass PrintStream or PrintWriter as argument to write the stack trace information to the file or stream.

Explain Java 7 ARM Feature and multi-catch block? If you are catching a lot of exceptions in a single try block, you will notice that catch block code looks very ugly and mostly consists of redundant code to log the error, keeping this in mind Java 7 one of the feature was multi-catch block where we can

catch multiple exceptions in a single catch block. The catch block with this feature looks like below: catch(IOException | SQLException | Exception ex){ logger.error(ex); throw new MyException(ex.getMessage()); } Most of the time, we use finally block just to close the resources and sometimes we forget to close them and get runtime exceptions when the resources are exhausted. These exceptions are hard to debug and we might need to look into each place where we are using that type of resource to make sure we are closing it. So java 7 one of the improvement was try-with-resources where we can create a resource in the try statement itself and use it inside the try-catch block. When the execution comes out of try-catch block, runtime environment automatically close these resources. Sample of try-catch block with this improvement is: try (MyResource mr = new MyResource()) { System.out.println("MyResource created in try-withresources"); } catch (Exception e) { e.printStackTrace(); } What is the difference between error and exception in java? Errors are mainly caused by the environment in which an application is running. For example, OutOfMemoryError happens when JVM runs out of memory. Where as exceptions are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access null object. In Java, both ClassNotFoundException and NoClassDefFoundError occur when a particular class is not found at run time. But, they occur at different scenarios. ClassNotFoundException is an exception which occurs when you try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath. On the other hand, NoClassDefFoundError is an error which occurs when a particular class is present at compile time but it was missing at run time.

Related Documents

Exceptions In Java
August 2019 39
Exceptions
November 2019 24
Exceptions
May 2020 19
Exceptions In Java.docx
December 2019 28
06 - Exceptions
June 2020 14

More Documents from "api-19624280"

Object Type Casting.docx
December 2019 2
Portugues Xxi 2.pdf
December 2019 5
Oops.docx
December 2019 4
Pro-e_file
June 2020 2
Exceptions In Java.docx
December 2019 28