Skip to main content

Posts

Showing posts from March, 2016

User defined exceptions in java

User defined exception in Java User defined exceptions   in java  are also known as  Custom exceptions . Most of the times when we are developing an application in java, we often feel a need to create and throw our own exceptions. These exceptions are known as  User defined or Custom exceptions.  In this tutorial we will see how to create and throw such  exceptions in a java program. Example of User defined exception in Java class MyException extends Exception { String str1 ; MyException ( String str2 ) { str1 = str2 ; } public String toString (){ return ( "Output String = " + str1 ) ; } } class CustomException { public static void main ( String args []){ try { throw new MyException ( "Custom" ); // I'm throwing user defined custom exception above } catch ( MyException exp ){ System . out . println ( "Hi this is my catch

Throw vs Throws in java

Difference between throw and throws in java In my previous tutorials I’ve covered below topics. In this post we are going to discuss difference between throw and throws in Java. Below are the links of my tutorials on throw and throws. I would highly recommend you to go through the below tutorials so that it would be easy for you to understand these terms. Throw in Java Throws in Java throw keyword example throws keyword example Throw vs Throws in java 1.  Throws clause  in used to declare an exception and  thow  keyword is used to throw an exception explicitly. 2. If we see syntax wise than  throw  is followed by an instance variable and  throws  is followed by exception class names. 3. The keyword  throw  is used inside method body to invoke an exception and  throws clause  is used in method declaration (signature). for example. Throw : .... static { try { throw new Exception ( "Something went wrong!!" ); } catch ( Exception exp ) {

Throws in Java

Throws keyword in Java 1. The  throws keyword is used in method declaration, in order to explicitly specify the exceptions that a particular method might throw. When a method declaration has one or more exceptions defined using throws clause then the method-call must handle all the defined exceptions. 2. When defining a method you must include a throws clause to declare those exceptions  that might be thrown but doesn’t get caught in the method. 3. If a method is using throws clause along with few exceptions then this implicitly tells other methods that – “ If you call me, you must handle these exceptions that I throw”. Syntax of Throws in java: void MethodName () throws ExceptionName { Statement1 ... ... } Example : public void sample () throws IOException { //Statements //if (somethingWrong) IOException e = new IOException (); throw e ; //More Statements } Note : In case a method throws more than one except

Throws Keyword in Java

Throws Keyword As we know that there are two types of exception –  checked and unchecked . Checked exceptions (compile time) are the one which forces the programmer to handle it, without which the program doesn’t compile successfully. While unchecked exception (Runtime) doesn’t get checked during compilation. “ Throws keyword ” is mainly used for handling checked exception as using throws we can declare multiple exceptions in one go. Let’s understand this with the help of an example. Example of throws Keyword In this example the method “mymethod” is throwing two  checked exceptions  so we have declared those exceptions in the method signature using  throws  Keyword. If we do not declare these exceptions then the program will throw a compilation error. import java . io .*; public class ThrowExample { void mymethod ( int num ) throws IOException , ClassNotFoundException { if ( num == 1 ) throw new IOException ( "Exception Message1" );

throw exception in java

throw Exception in Java In java we have already defined exception classes such as  ArithmeticException  ,  ArrayIndexOutOfBoundsException  ,  NullPointerException  etc. There are certain conditions defined for these exceptions and on the occurrence of those conditions they are implicitly thrown by JVM(java virtual machine). throw an already defined exception like  ArithmeticException , IOException  etc. Syntax of throw statement throw AnyThrowableInstance ; Example: //A void method public void sample () {   //Statements   //if (somethingWrong) then   IOException e = new IOException ();   throw e ;   //More Statements } Note : A call to the above mentioned sample method should be always placed in a try block as it is throwing a  checked exception  –  IOException . This is how it the call to above method should be done: MyClass obj = new MyClass (); try { obj . sample (); } catch ( IOException ioe ) { //Your error Message here