Exception Handling Tutorials in Java

0
170
Spread the love
  • 59
  • 85
  • 26
  • 89
  •  
  •  
  •  
  • 14
  •  
  •  
    273
    Shares

Take care to document each checked and unbridled exceptions. Since unbridled exceptions commonly represent programming errors, documenting them will keep us from avoiding those errors. we will be able to use constant Javadoc tag @Throws to document unbridled exceptions, as is that the case for checked exceptions.

If multiple ways during a category will throw constant exception, we will document the exception once within the class’s documentation comment. this is often rather more economical than documenting the exception severally for every of the ways. Interviewer may cross question here-part of exception in java interview questions.

Throw early catch later principal

As a java developer, we need to follow throw early and catch later principal.

 

Few standard Exceptions: Good to know for exception in java interview questions

How We Loose Exception Information in Java?

Exception information lost
Exception information lost

This is also very important aspect of exception in java interview questions as well as exception in java tutorial.We can also throw a different Exception than one we caught.Once we do that we get a similar effect as when using fillInStackTrace().The original information of site of Exception is lost . The information we will have the new Exception from the position we throw the Exception.

I have written the throw keyword and its functionality in detail.
let us check the code to see exception handling in java with examples. This is exception handling in java best practices.


public class RethrowNew{
public static void main(String []args){
try{
myMethod();
}
catch(Exception e)
{
System.out.println("Caught in main");
e.printStackTrace();
throw new NullPointerException("From main");
}
}
public static void myMethod() throws Exception{
System.out.println("Originatring from myMethod()");
throw new Exception("from myMethod()");
}
}

The output of the code:
$javac RethrowNew.java
$java -Xmx128M -Xms16M RethrowNew
Originatring from myMethod()
Caught in main
java.lang.Exception: from myMethod()
at RethrowNew.myMethod(RethrowNew.java:16)
at RethrowNew.main(RethrowNew.java:5)
Exception in thread “main” java.lang.NullPointerException: From main
at RethrowNew.main(RethrowNew.java:11)
The final Exception only knows that it has come from main() method not from myMethod().Throwable is not necessary in any of the Exception operations.We never have to worry about cleaning up the previous Exceptions or any of the Exception.As they are created by the keyword new,Garbage collector will automatically clean them.

This is what I have gathered on exception in java tutorial and exception in java interview questions. Hope you enjoyed my article.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 59
  • 85
  • 26
  • 89
  •  
  •  
  •  
  • 14
  •  
  •  
    273
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here