-->

  • Exceptions in Java

     

    Exceptions in Java

     

    Introduction


    Customers have high expectations for the code we produce.

    Users will use our programs in unexpected ways.

    Due to design errors or coding errors, our programs may fail in unexpected ways during execution.

    It is our responsibility to produce quality code that does not fail unexpectedly.

    Consequently, we must design error handling into our programs. 

     

    Errors and Error handling


    An Error is any unexpected result obtained from a program during execution.

    Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination.

    Errors should be handled by the programmer, to prevent them from reaching the user.


    Memory errors (i.e memory incorrectly allocated, memory leaks…)

    File system errors (i.e. disk is full…)

    Network errors (i.e. network is down…)

    Calculation errors (i.e. divide by 0)

    Array errors (i.e. accessing element –1)




    Types of Exceptions


    Unchecked Exceptions

    Checked Exceptions It is not required that these types Must either be caught by a of exceptions be caught or method or declared in
    its declared on a method. signature.


    Runtime exceptions can be

    Placing exceptions in the generated by methods or by method signature harkens the JVM itself.

    Errors are generated from deep within the JVM.


    Keywords


    • throws Describes the exceptions which can be raised by a method.
    • throw Raises an exception to the first available handler in the call stack, unwinding the stack along the way.
    • try Marks the start of a block associated with a set of exception handlers.
    • catch If the block enclosed by the try generates an exception of this type, control moves here; watch out for implicit subsumption.
    • finally Always called when the try block concludes, and after any necessary catch handler is complete.



    There are two types of errors:


    Compile time errors
    Runtime error



    Compile time errors

    Instead of declaring int a; you mistakenly declared it as in a; for which compiler will throw an error.



    Runtime Errors Example

    A Runtime error is called an Exceptions error. It is any event that interrupts the normal flow of program execution.

    Example for exceptions are, arithmetic exception, Nullpointer exception, Divide by zero exception, etc.



    class JavaException {
       public static void main(String args[]){
          int d = 0;
          int n = 20;
          int fraction = n/d;
         System.out.println("End Of Main");
       }
    



     

     

    Disclaimer

     

    All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.

     

     

  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.