Try And Catch.docx

  • Uploaded by: Shubham Chauhan
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Try And Catch.docx as PDF for free.

More details

  • Words: 245
  • Pages: 3
TRY AND CATCH  The try statement lets you test a block of code for errors.  The catch statement lets you handle the error.  The try statement allows you to define a block of code to be tested for errors while it is being executed.  The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Syntax of Java Try-Catch try{ //code that may throw an exception }catch(Exception_class_Name ref){}

EXAMPLES OF TRY AND CATCH: INPUT : class Example1 { public static void main(String args[]) { int num1, num2; try { /* We suspect that this block of statement can throw * exception so we handled it by placing these statements * inside try and handled the exception in catch block */ num1 = 0; num2 = 62 / num1; System.out.println(num2); System.out.println("Hey I'm at the end of try block"); } catch (ArithmeticException e) { /* This block will only execute if any Arithmetic exception * occurs in try block */ System.out.println("You should not divide a number by zero"); } catch (Exception e) { /* This is a generic Exception handler which means it can handle * all the exceptions. This will execute if the exception is not * handled by previous catch blocks.

*/ System.out.println("Exception occurred"); } System.out.println("I'm out of try-catch block in Java."); } }

OUTPUT: You should not divide a number by zero I'm out of try-catch block in Java.

Related Documents

Try
June 2020 20
Try
October 2019 26
Try
November 2019 14
Try
May 2020 10
Try
November 2019 30
Try And Catch.docx
December 2019 4

More Documents from "Shubham Chauhan"

Java Assignment.docx
December 2019 9
Try And Catch.docx
December 2019 4
Rent Agreement.pdf
May 2020 27
Final.docx
May 2020 0
Policy Copy.pdf
May 2020 1