printStackTrace in java

It prints the short description of the exception(using toString()) + a stack trace for this exception on the error output stream(System.err). Constructors public void printStackTrace() public void printStackTrace(PrintStream s) public void printStackTrace(PrintWriter s) Example: printStackTrace() public class Main {   public static void main(String[] args) { int num1= 35, num2 = 0;   try { … Read more

Superclass of all exception classes

The java.lang.Throwable is the superclass of all exception classes in java. Hierarchy Exception Hierarchy Error Example public class Main {   public static void main(String[] args) { int num1= 30, num2 = 0;   // Try to divide by zero try { int result = num1 / num2; } catch (Throwable e) { System.out.println("Exceptionn class: … Read more

StackOverflowError in java

StackOverflowError in a runtime error in java. It is thrown when an application recurses too deeply. Whenever the method execution happens a call stack with limited amount of memory location is assigned. This amount of memory is allocated by JVM. Based upon the number of the method execution the size of the call stack grows … Read more

ClassCastException in java

ClassCastException is a RunTimeException or unchecked exception which occurs when JVM unable to cast an object of one type to another type. Constructors of ClassCastException ClassCastException() It creates an instance of the ClassCastException class. ClassCastException(String s) It creates an instance of the ClassCastException class by using the specified string as message. Note: ClassCastException is related … Read more

Final, finally and finalize in java

final: final in java represents a keyword which can be used to apply restrictions on variable, method and class. It can be used with instance variables, local variables, methods and classes. Important points If we use final with class then it would not be possible to extend that class i.e. Final class will not be … Read more

throw and throws in java

throw: throw is used in re-throwing exception process or we can say that it is used to throw an exception object explicitly. It can take at most one argument which will be an exception object. Only unchecked exception can be thrown. Syntax: throw exceptionObject; Example public class Main {   public int reminderTest(int number1, int … Read more

Rethrowing an exception in java

Normally, catch block are used to handle the exceptions raised in the try block. The exception can re-throw using throw keyword, if catch block is unable to handle it. This process is called as re-throwing an exception. public class Main {   public int test(int n1, int n2) { try{ return n1/n2; }catch(ArithmeticException e){ throw … Read more

can we throw an exception without throws?

Yes, we can throw an exception manually using throw keyword without throws. Syntax: throw InstanceOfThrowableType; public class Main {   public int test(int n1, int n2) { if(n2 == 0){ throw new ArithmeticException(); } else { return n1/n2; } }   public static void main(String[] args) { Main main = new Main(); try{ System.out.println(main.test(130, 0)); … Read more

will finally block get executed if return?

Yes, finally block will always be executed. Finally block will always execute except the below cases: Call the System.exit() method explicitly, power failure, JVM crash, software crash etc. public class Main {   String getWebsiteName() { try { return "w3schools.com"; } catch(Exception e) { return "java.com"; } finally { System.out.println("Finally block executed even after a … Read more

ClassNotFoundException vs NoClassDefFoundError in java

ClassNotFoundException ClassNotFoundException is an exception that occurs when we try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath. public class Main {   public static void main(String[] args) { try { Class myClass = Class.forName("test.MyTest"); System.out.println("Class " + myClass + " found … Read more