Question:

What are user defined exceptions in Java?

by Guest5988  |  12 years, 8 month(s) ago

0 LIKES UnLike

Wondering about the details of what is user defined exceptions in Java? Please help and thank you in advance.

 Tags: Defined, Exceptions, Java, user

   Report

1 ANSWERS

  1. Guest6235

     The Java dialect is conceived to handle mistakes and other unforeseen happenings utilising an exclusion concept. When a mistake happens, an exclusion object is developed and passed (or thrown) up the call stack until it finds a procedure conceived to handle (or catch) that exception. Java arrives with dozens of exclusion things, but programmers can furthermore conceive their own.

    Defining a Custom Exception Class

    Custom exclusions are characterised in the accurate identical way as any other Java class. The only significant direct is that exclusion should continue the Exception class in the java.lang library. This is finished in the identical way that any other class expands another. A demonstration of client characterised exclusion would be:

    class MyException extends Exception {

    }

    You can add your own procedures and constructors to your exclusion class in alignment to customize it to your reasons, but just by expanding the groundwork class you will get the benchmark exclusion constructors and procedures, encompassing the proficiency to characterise mistake messages.

    Throwing a Custom Exception

    To use your made-to-order exclusion, you have to hurl it somewhere in the program, the identical as any other exception. For demonstration, the next trivial demonstration shows how to hurl an exclusion to avert a procedure from splitting up by zero:

    public double divide(double dividend, double divisor) throws MyException {

    if (divisor == 0) throw new MyException("Division by zero attempted!");

    return dividend/divisor;

    }

    The difficulty tests to glimpse if the divisor is identical to zero. If it is, then a new MyException object is conceived and hurled with a befitting mistake message. Whatever part of the program called the "divide" function would then be to blame for concluding how to deal with the exception.

    Why Use Exceptions?

    Error ascertaining is a critical part of any program. Even if the programmer's work is flawless, input from the client, the localized document scheme and the Internet is seldom, if ever, flawless, so programs should be arranged to deal with problems. Often, although, the part of the cipher that finds the difficulty is not the part of the cipher best matched to deal with the problem. A function that splits up a paragraph of text into phrases should, perfectly, not have to concern about if an empty paragraph is an mistake that needs concluding the program, a odd happening that should be described to the client, or irrelevant. Exceptions permit it to report the difficulty to other components of the program that are better matched to amending it.

    Why Use Custom Exceptions?

    Java arrives with dozens of exclusions categories predefined, although, occasionally it assists to have a set of exclusions that better fit the difficulty at hand, in order that the program can more gladly answer to them. For demonstration, other than Java's generic "InvalidArugmentException," the part of the cipher to blame for retrieving from mistakes may require to understand more minutias about the problem. This could be finished by splitting up the exclusion into made-to-order "UnexpectedNullArgumentException" and "OutOfRangeException."

Sign In or Sign Up now to answser this question!

Question Stats

Latest activity: 13 years ago.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions