exception – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Thu, 08 Apr 2021 15:32:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg exception – Programmerbay https://programmerbay.com 32 32 Explain Exception Handling in Java https://programmerbay.com/explain-exception-handling-in-java/ https://programmerbay.com/explain-exception-handling-in-java/#respond Sat, 14 Sep 2019 18:07:22 +0000 https://programmerbay.com/?p=5160 As we discussed, Exceptions are errors that abrupt the normal flow of a program.  It often occurs when constraints or rules of that language get violated.

These exceptions can be handled by creating an exception handler. Try, catch, finally and resources are the components that help to create an efficient and effective exception handler, they rescue a program from being crashed.

Try Statement

  • This is the very first step to construct an exception handler. It encloses all critical statements which might trigger an exception.
  • Try block monitors each & every critical statement. If a constraint gets violated, then it would throw an exception object.
  • A Try can have multiple catch statements.
  • It always associates with either a catch statement or a finally statement, forming a pair such as try-catch or try-finally.
try{

//  code here

}

Catch Statement

  • A catch statement is an exception handler that handles the exception object thrown by try statement.
  • Thrown exception object is caught by an appropriate catch block.
  • It accepts an argument which represents an exception object of the specific type.
  • A single catch statement can handle multiple exceptions
  • No code can be put in between try and catch statement. Meaning, once a try block ends up, immediately after that, a catch or finally block should be there in a program.
  • If a try block has multiple catch statements, then the compiler would choose that catch block which keeps the appropriate type of that exception object. catch (ExceptionType objName){ // code }  

]]>
https://programmerbay.com/explain-exception-handling-in-java/feed/ 0
Java Exception handling and its hierarchy https://programmerbay.com/java-exception-handling-and-its-hierarchy/ https://programmerbay.com/java-exception-handling-and-its-hierarchy/#respond Sat, 14 Sep 2019 17:29:01 +0000 https://programmerbay.com/?p=5113 What is an Exception?

An exception is an event that occurs when a program fails to achieve the normal flow which leads to the termination of that program. In other orders, When a program doesn’t follow rules or constraints defined in Java language then it is said to be an exception.

What happens when an exception occurred?

new exception
  • When an exception is triggered, the method in which it has occurred creates an object, referred as exception object that contains the information of what type of exception this is and state of the program.
  • After creating an exception object, it hands over to JRE,  this stage is termed as throwing an exception.
  • Now, JRE keeps looking for an appropriate method having a block of codes from call stack (list of methods) to handle that exception called exception handler by tracing back all the way from the method where an error occurred to the appropriate exception handler.
  • Ones an exception handler is found, the exception object is thrown to that handler, this is known as catching an exception. if no handler is found for it, then the program would be terminated.
what happen is

Hierarchy of Exception

The throwable class covers entire exception handling in Java.

Exception Hierarchy

 Throwable class can be classified into two subclasses:-

Exception:  An exception which occurs when the problem is specifically triggered by our program and disrupts the normal flow of that program. For example, ArrayOutOfBound exception, I/O exception and more. In other words, an exception is often caused by a program itself. It is recoverable.

An exception can also be divided into two type:-

  • Unchecked Exception: These exceptions are those exceptions which are not identified at compile time by the compiler. For example, RuntimeException.
  • Checked Exception: Checked exceptions are those exceptions which are identified at the compile time by the compiler. For example, FileNotFound exception.

Error: An error occurs when the system in which our JRE executes has insufficient resources for our own program. For example, stack overflow. It is not recoverable.

]]>
https://programmerbay.com/java-exception-handling-and-its-hierarchy/feed/ 0