Results 1 to 18 of 18
  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Use of user defined exception

    I want to know what is the advantage of user defined exception.

    If just giving a message is a benefit of user defined exception, it can also be done by using built in exception, as in Code 1.

    Code 1:
    Java Code:
    public class Main {
    	public static void main(String[] args) {
    		try {
    			int haveAccess=1;
    
    			if(haveAccess==0)
    			{
    				throw new Exception("dont have access.");
    			}
    			else
    			{
    				throw new Exception("report not generating");
    			}
    		} catch (Exception e) {
    			System.out.println(e.getMessage());
    		}
    	}
    }
    Last edited by jmohandos304; 10-06-2014 at 01:42 PM. Reason: changed 1 to 0 in if clause

  2. #2
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    Sorry confused!!! Now Clear.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Use of user defined exception

    Not certain what "Now clear" means. You didn't really use a user defined exception. To do that you should subclass Exception or perhaps
    subclass an existing exception and use that. The string simply explains more about the offending exception. And sometimes you may want to
    re-throw lower level exceptions thrown from JDK classes so they are more relevant to the higher level code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    Ok, so that the thread does not go waste.

    The code mentioned in post 1 is not user defined exception according to you. Then is it built-in exception or what exactly it is?

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Use of user defined exception

    Exception is a class in the JRE/JDK. "User-defined" is open for interpretation, but the majority of Java developers will understand it as:
    Java Code:
    class MyApplicationException extends Exception {
       public MyApplicationException(String message, Throwable cause) {
          super(message, cause);
       }
    }
    If you define "user-defined" as "an exception with a reason that the developer (user) set", then yes, it could be user-defined. I would say a user-defined exception is one that extends Exception.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    Perfect. Thanks.

    But that means built-in exception can have user-defined messages. In that case what is the use of user-defined exceptions?
    Last edited by jmohandos304; 10-08-2014 at 01:27 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Use of user defined exception

    Quote Originally Posted by jmohandos304 View Post
    But that means built-in exception can have user-defined messages. In that case what is the use of user-defined exceptions?
    Exceptions can carry a lot more information than just a message (see its API documentation).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Use of user defined exception

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    Exceptions can carry a lot more information than just a message (see its API documentation).

    I am assuming you are talking about built-in exceptions, since you are mentioning API. Back to the same question. If built-in messages are detailed enough, why we need user defined exceptions in the first place? What is the advantage?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Use of user defined exception

    Quote Originally Posted by jmohandos304 View Post
    Exceptions can carry a lot more information than just a message (see its API documentation).

    I am assuming you are talking about built-in exceptions, since you are mentioning API. Back to the same question. If built-in messages are detailed enough, why we need user defined exceptions in the first place? What is the advantage?
    OK, so say when catching an Exception i want to know exactly what the exception is about, since I handle exception X differently to exception Y.
    Which is easier and (more importantly) more robust?
    Java Code:
    try {
        doSomething();
    } catch (Exception e) {
        if (exception.getMessage().equals("This is the text I'm looking for")) {
            // I know what to do here so process this problem
        } else {
            // No idea, rethrow:
            throw e;
        }
    }
    or:
    Java Code:
    try {
        doSomething();
    } catch (MyException e) {
        // I know what to do here so process this problem
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    Advantages of Exceptions (The Java„ Tutorials > Essential Classes > Exceptions)

    The above link does not talk about user defined exceptions. I am talking about the need for user-defined exceptions(the one where we create a class representing the exception and which extends the general Exception class) when a message can be printed(as in Code 1 in post 1) by using built-in exceptions.

  12. #12
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    OK, so say when catching an Exception i want to know exactly what the exception is about, since I handle exception X differently to exception Y.
    Which is easier and (more importantly) more robust?
    Java Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    try {
    doSomething();
    } catch (Exception e) {
    if (exception.getMessage().equals("This is the text I'm looking for")) {
    // I know what to do here so process this problem
    } else {
    // No idea, rethrow:
    throw e;
    }
    }
    or:
    Java Code:
    1
    2
    3
    4
    5
    try {
    doSomething();
    } catch (MyException e) {
    // I know what to do here so process this problem
    }


    Thanks Tolls. Now I see the advantange of user defined exceptions.

  13. #13
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    Moderators, I am not able to post new threads. It says page not found. Please help.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Use of user defined exception

    Did you click 'Post New Thread' near the top left of the thread listing page? I just tried it and it works fine for me ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  15. #15
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Use of user defined exception

    Thanks for the reply. Pls see attachment.
    Attached Thumbnails Attached Thumbnails Use of user defined exception-pagenotfound.png  

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Use of user defined exception

    This has happened to me several times before (several days ago). It seems to correspond with connectivity issues
    when I could not connect to the site. I finally got in but kept getting this message when I was replying to an
    existing thread. It eventually resolved itself but I don't know what really caused the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Use of user defined exception

    Everything went fine for me (I'll remove that test thread after posting this reply); my guess is that it is a connection/client issue ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Use of user defined exception

    Going back to the original topic, consider a situation in which multiple exceptions are thrown and caught.

    Java Code:
    try {
       // some block of code
    } catch (FileNotFoundException fne) {
       // handle appropriately
    } catch (SomeOtherException soe) {
       // handle appropriately
    } catch (YetAnotherException yae) {
       // handle appropriately
    } catch (Exception e) {
       // anything remaining
    }
    If you just used Exception, it would require additional logic to isolate and handle
    the various exceptions.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. User-Defined Classes
    By Reptyle216 in forum New To Java
    Replies: 3
    Last Post: 12-03-2013, 10:31 AM
  2. Replies: 3
    Last Post: 08-14-2013, 12:19 PM
  3. Set of user defined class
    By TelliameD in forum New To Java
    Replies: 4
    Last Post: 07-05-2012, 10:58 AM
  4. Problem with user defined class
    By moosethmucha in forum New To Java
    Replies: 8
    Last Post: 04-17-2012, 06:58 PM
  5. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 02:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •