Exception:
An exception is an error that occurs during runtime of a program. Example can be like we are using a array index that is out of bound of defined array in a program.
Exception Handling:
Exception handling is used to handle runtime exception . It uses try,catch and finally block to handle exceptions.
- Try block:
We put that code in try block in which exception can arise.
- Catch block:
We put catch block to handle the exception that are in try block as it catches exception from try block and we write some code inside catch block to handle exceptions.
- Finally block:
We can use finally block in place of catch block or if catch block is present then we can also write finally block. Finally block will always execute regardless of whether their is exception or not in a program or script.
Example:
<script>
try {
adddalet("Welcome to FindNerd!");
}
catch(err) {
document.getElementById("demo").innerHTML = err.message;
}
</script>
In the above example we have misspelled "alert" so there will arise a exception and it is handled in catch block.
0 Comment(s)