
Usage of Try and Catch Blocks: Exception Handling Mechanism 7.5
7.5 Usage of Try and Catch Blocks: Exception Handling Mechanism
Java raises an exception object. For it to do so, we need to use a try block, wherever we expect
likely exception. Catch block that follows the try block catches the exception object and takes
remedial action.
Example 7.1: Use of Try and Catch Block. Allocation of Memory for a Two- dimensional
Matrix
1. package com.oops.chap7;
2. import java.util.*;
3. class MatrixAlloc {
4. void ReadMat(int A[][],int rows, int cols){
5. //input matrix related data using scanner
6. Scanner scn=new Scanner(System.in);
7. for ( int i=0;i<rows;i++) ...