Exceptions

What is the difference between a checked and unchecked exception?

  • Checked the exceptions are checked at compile time, example file not found exception.
  • Unchecked exceptions are checked at run time, example null pointer exception.
  • Checked exceptions are Many to be handled in code.

How exceptions are handled in overriding methods?

  • If superclass method has not declared any exception using throws clause, then subclass overridden method can’t declare any checked exception, but it can declare unchecked exception with the throws clause.
  • If super class method has declared a checked exception Using throws clause Then sub class overhead and method can do one of the three things
    • Sub class can declare the same exception as declared in the superclass method.
    • Subclass can declare the subtype exception declared in the superclass method.
    • Subclass method can choose not to declare any exception at all

What is the custom exception?

  • A custom exception is created by the developer itself to perform custom tasks On exception.
  • All custom exceptions Extend the exception class.
  • Just create a constructor with the input String parameter.
  • And Call, the super method inside the constructor with a string.

Exception caused by Out of memory Error Caused by Heap

  • Out of memory error Is thrown by Java virtual machine(JVM) When an object cannot be allocated due to lack of memory space and also the garbage collector cannot free some space.
  • The out of memory error extends virtual machine error class , Which indicates that the JVM is broken, Or it has run out of his Resources and cannot operate.
  • A virtual machine error extends error class, Which is used to indicate those serious problems that an application should not catch. A method may not declare such errors In its throw clause, Because these are abnormal conditions that shall never occur.
  • To resolve, try to increase the heap size Of the JVM By using runtime parameter -XMX 1024M We have 1024 is the space to be allocated to the heap in megabytes. This is used to provide maximum heap size.
  • We can also use -X to Increase or decrease, meta space size.
  • Another parameter we can use is -XMS 256M This is used to provide minimum heap size
  • Unreferenced objects Not collected by garbage collector Cause a memory leak and this error.
  • Reasons of out of memory error
    • Heap size is maximum reached and JVM is unable to create new object.
    • Meta space is used to store Static classes or static variable. Before Java 8 It was called as permanent generation space. When our program has more number of static class or static field then automatically avoid meta space size will increase.
      • When meter space size, which is maximum size, it throws out of memory error.
    • If requested array size exceeded VM limit.
    • If we do Much swapping in our application, we get out of swap space error.
    • If we call a lot of native methods, we get out of memory error.
  • Fixes for out of memory error
    • Implicitly open resources in try block so that they are released automatically once operation is done.
    • Dereference objects which are not in use.
    • If resources are not opened implicitly in try, block, implicitly, use finally to close them.
    • Increase the heap size.
    • Use reference objects to avoid memory leaks Which can be easily cleared by garbage collector.
    • Reference has three sub classes
      • Phantom reference
        • Garbage collector would not be able to automatically cleanup Phantom reference object, We need to clean them up manually and all references to it.
        • Phantom reference works with reference Queue.
        • Garbage collector adds phantom defence to Queue After finalise method is executed. It implies instance is still in the memory.
        • Uses
          • It is used in cases when an object has to be removed from memory. We can schedule memory, sensitive tasks.
          • Avoid using finalise method and improve finalisation process.
      • Soft Reference
        • Soft reference objects are cleared by garbage collector when memory runs low.
      • Weak reference
        • Weak reference objects when sensed by garbage collector are cleared and ultimately taken out of memory.
    • Release the session when it is no longer needed
      • HttpSession.invalidate()
    • Keep the time out of session for each session.
    • Avoid using too much string Concatenation. Use string buffer instead To append strings. A large number of objects will slowdown the performance.
    • If a query is being used too much frequently use prepared statement rather than statement
      • Prepared statement is pre compiled, while statement is compiled every time elude statement is transferred to database.
    • When using JDBC code, avoid using “*” Within your query, try to use specific columns instead.
    • If prepared statement is used within a loop, then it should be closed in the loop.
    • Be sure to close result that, connection, prepared statement, and statement in finally block.
  • There are times when your application may crash without giving error, Check log dumps and crash dumps in such case.
  • Java profiling helps you to monitor JVM parameters, including object creation, Thread execution, Method execution, And yes, garbage collection.
Class Cast Exception

No comments:

Post a Comment