Ticker

6/recent/ticker-posts

Exceptions in Java & Try Catch Block



Exceptions: are events that occur during program execution and interrupt the flow of the program. Whenever an exception occurs, an object of the exception is created, which contains the information about the exception, such as the type of exception and the state of the After the object is created, it is thrown in the method that caused the exception. The Java runtime system then searches for the exception handling techniques in the method that caused the exception. If no exception handling technique is found, the Java runtime system searches the class for exception handling techniques in which the method is written. In case no technique is used to handle exceptions, the Java runtime system terminates and the program is interrupted. In Java, exceptions are handled by using the try, catch, throw, and throws clauses. All exceptions are broadly divided into two categories: checked exceptions and unchecked exceptions. They are the exceptions that a well-written program can catch and notify the user by providing the reason why the exception has occurred.

For example, the Arithmetic Exception exception will notify the user that the exception has been generated due to an error in arithmetic calculation. All the exceptions in the Throw able class, except for those that come under the Error, Runtime Exception classes and their subclasses, are considered as checked exceptions. Runtime Exception is the subclass of the Exception subclass.

 

Exception Handling Techniques:

In Java, we have various techniques to handle exceptions. These techniques use various clauses such as Try, catch, throw. You can use these techniques to create programs in which the exceptions are properly handled. This, in turn, helps in creating well designed applications. The following techniques are used to handle exceptions:

 *The try and catch blocks

 *Multiple catch blocks

 *Nested try blocks

 *The throw clause

 *The throws clause

 

Using Try Catch Block: When you write a program, you may observe certain statements in it that may raise exceptions. You must enclose such statements inside a try block. You enclose the code that you want to monitor inside a try block to handle a runtime error. It also contains the messages telling the user the reason why the exceptions occurred. It’s easy to resolve an exception after its cause is known. The following snippet shows you how to use the try and catch blocks:

Multiple Try Catch Block we used only one catch block to handle exceptions and Exception, the super class of exceptions to perform exception handling. However, if you have a good idea of the kind of exceptions that may cause the program to be interrupted, you can use multiple catch blocks to handle these exceptions separately. It is likely that, like most of us, you may forget to include some exceptions in the catch clauses that may lead to program interruption or termination. To prevent this from happening, use the Exception super class in the last catch block so that all the other remaining exceptions can be handled. The Exception class must be used in the last catch block; otherwise, an error will occur during compilation because a super class cannot be used before a subclass.


For more understanding you can also read below mentioned topic from online:

exception handling try catch block in c#


Post a Comment

0 Comments