Java Exception propagation

Exception propagation is a way of propagating exceptions from method to method. Let an exception occur in a method at the top of the call stack and if it is not caught then it propagates to the previous method in the call stack, if it is not caught here then it again propagates to the … Read more

Java throw keyword

throw is used to throw an exception object explicitly. It can take only one argument and that will be an exception object. Exception object to be thrown can be of either checked or unchecked exception type.   Syntax throw exception_object;   Java throw keyword Example class ArithmaticTest{ public void division(int num1, int num2){ try{ //java.lang.ArithmeticException … Read more

Java Multiple catch blocks

If more than one exception can occur in one try block, then we can use multiple catch blocks to provide appropriate handlers to different exception objects. Note: in case of multiple catch blocks, blocks must be placed from specific handler to general handler.   Syntax of try block with multiple catch blocks: try{ //block of … Read more

try and catch blocks in java

try block try block is used to enclose the code that might throw an exception. It must be followed by either a catch or finally or both blocks.   Syntax of a try block with catch block try{ //block of statements }catch(Exception handler class){ }   Syntax of try block with finally block try{        … Read more

Exception handling in java

Exception handling is a mechanism to handle runtime errors so that the normal flow of the program can be maintained.   Exception Hierarchy Throwable is the superclass.    Advantages/Benefits of Exceptional Handling Using exceptional handling we can separate the error handling code from normal code. Using exceptional handling we can differentiate the error types. The … Read more

Java StringBuilder ensureCapacity() method

ensureCapacity(int minCapacity): ensures that the capacity is at least equal to the specified minimum.   Syntax: public void ensureCapacity(int minCapacity)   Note: If the current capacity is greater than the argument there will be no change in the current capacity. If the current capacity is less than the argument there will be a change in the current … Read more

Java StringBuilder capacity() method

capacity(): returns the current capacity of the string builder. Capacity refers to the amount of available storage.   Syntax: public int capacity()   StringBuilder capacity() Example class TestStringBuilder{ StringBuilder sb = new StringBuilder(); public void capacityTest(){ //default capacity. System.out.println(sb.capacity()); sb.append(“Hello “); //current capacity 16. System.out.println(sb.capacity()); sb.append(“www.hello.com”); //current capacity (16*2)+2=34 i.e (oldcapacity*2)+2. System.out.println(sb.capacity()); } } public … Read more