Java Tutorial – 7 | Edureka #Advanced java Tutorial, #Java, #java training, #java course, #java Class

Take instructor-led Live class on Java Tutorial at :
http://www.edureka.co/java-course

The following topics were covered in this Java Tutorial:
Exception, Exception handling, Throw and Throws keyword, Finally keyword, Runtime Exception, Error, Error handling.

Exceptions:
Exception is the one that stops the execution of the program unexpectedly. All of the sudden the program stops and returns an error. The process of handling these exceptions is called Exception Handling.
If you want to write a program and if you know that an exception can occur at a particular point of code then you can handle that exception using some mechanisms. Now that application will not stop even if an exception occurs in the program. This is the advantage of exception handling.
Types of Exceptions:
Run-time exceptions: These exceptions will occur at run- time i.e when you run your application. Due to this exception your java application will halt. These exceptions are also called as Unchecked Exceptions. These exceptions are handled at run-time i.e by JVM after they have occurred by using try and catch block. Example: ArrayIndexOutOfBoundsException, ArithmeticException, NullPointerException
Compile Enforced Exceptions: Before running, the compiler itself tells you that there is a chance of getting an exception or something wrong with your program. These exceptions are also called as Checked Exceptions. These exceptions are handled by java complier before they occur by using throws keyword. Example: IOException, FileNotFoundException.

Exception Classes:
Once you have an object there is “Throwable” class. Within which you have two things Error and Exception. Again the exceptions are of two types: compile-enforced exception, Run-time exception.
Exception Handling Mechanism:
There are 3 ways to handle the run-time exceptions: try block, catch block, finally block.In try block we try some part of the code where you feel that an exception will occur. If an exception has occurred then that exception will be sent to catch block to handle. If no exception arises then the program will execute normally.

“throw” is a keyword which is used to call the sub class of an exception class. This keyword is also used to throw the exception occurred in try block to catch block. While throwing an exception you can also send message to catch block which has to be printed when an exception occurs.
Finally:
When we want a set of statements to be executed even after an exception has occurred then we use finally block.
throws:
To handle the compiler-enforced exceptions we use “throws” keyword. “throws” is applied to methods or a class for which there is a chance of rising an exception during its execution.

User-defined Exception:
Across built-in exceptions user can also define his own exceptions. It can be done by defining a class that extends Exception class and creating a constructor of the class (user-defined) with string argument to print that message when exception occurs. Here why we need to string argument to the constructor because every exception will return a message in the form of a string.

Error and Error Handling:
There are two types of error: Design- time error and Logical error.

Design-time error: These are the errors that occur while designing the programs. Example for Design errors is syntax errors. These errors will be shown with a red mark in eclipse IDE so that you can easily find and correct it. Missing a semicolon will return design time error.

Advantages of Exception:
The program will still execute even if an exception arises i.e finally block i.e if you need a part of code to be executed necessarily then place that code in finally block.

If you can’t handle the exception JVM will handle the exception if we use throws keyword.

We can differentiate the exceptions that have occurred. If you get array index out of bound exception then it indicates that we are trying to perform an action on the array element exceeding the size of the array. When you divide a number by zero you will get an arithmetic exception. So you can differentiate the exceptions that have occurred.

Logical error: These are the errors done by programmer. Even though the program is syntactically correct the program will not produce expected output i.e. the programs with these errors will run but does not produce desired results. Such type of errors is called logical errors. Example for logical error is getting division of two numbers as output instead of expected output of multiplication of numbers. These errors can be rectified by understanding the logic and checking whether it is works out correctly or not.

Please write back to us at support@edureka.in or call us at +91-8880862004 for more information.

Visit – http://www.edureka.co/java-course 원문보기

2019/12/01 08:21

Uploaded By @Vlogger

시력게임 :: 눈이 건강해지는 앱 [아이앱]