మినహాయింపు రకాలు

"నేను ఈ రోజు మరొక విషయం గురించి మాట్లాడాలనుకుంటున్నాను. జావాలో, అన్ని మినహాయింపులు రెండు రకాలుగా విభజించబడ్డాయి: తనిఖీ చేయబడినవి మరియు తనిఖీ చేయనివి (తప్పక పట్టుకోవాల్సినవి మరియు మీరు పట్టుకోవాల్సినవి లేనివి) డిఫాల్ట్‌గా, అన్ని మినహాయింపులు ఉండాలి పట్టుకున్నారు."

"మీరు ఉద్దేశపూర్వకంగా మీ కోడ్‌లో మినహాయింపులను వేయగలరా?"

"మీరు మీ స్వంత కోడ్‌లో ఏదైనా మినహాయింపును వేయవచ్చు. మీరు మీ స్వంత మినహాయింపులను కూడా వ్రాయవచ్చు. అయితే మేము దాని గురించి తరువాత మాట్లాడుతాము. ప్రస్తుతం, జావా మెషిన్ ద్వారా విసిరిన మినహాయింపులపై దృష్టి పెడదాం."

"అలాగే."

" క్లాస్‌నాట్‌ఫౌండ్‌ఎక్సెప్షన్ లేదా ఫైల్‌నాట్‌ఫౌండ్‌ఎక్సెప్షన్‌ను ఒక పద్ధతిలో విసిరివేసినట్లయితే (సంభవిస్తే), డెవలపర్ తప్పనిసరిగా వాటిని మెథడ్ డిక్లరేషన్‌లో సూచించాలి. ఇవి తనిఖీ చేయబడిన మినహాయింపులు. ఇది సాధారణంగా ఇలా కనిపిస్తుంది:"

తనిఖీ చేయబడిన మినహాయింపుల ఉదాహరణలు
public static void method1() throws ClassNotFoundException, FileNotFoundException
public static void main() throws IOException
public static void main() // Doesn't throw any exceptions

"కాబట్టి మనం 'త్రోలు' తర్వాత కామాతో వేరు చేయబడిన మినహాయింపుల జాబితాను వ్రాస్తాము, సరియైనదా?"

method1"అవును. కానీ ఇంకా చాలా ఉన్నాయి. ప్రోగ్రామ్ కంపైల్ చేయడానికి, దిగువ ఉదాహరణలో కాల్ చేసే పద్ధతి తప్పనిసరిగా రెండు విషయాలలో ఒకదాన్ని చేయాలి: ఈ మినహాయింపులను క్యాచ్ చేయండి లేదా వాటిని (కాలర్‌కి) తిరిగి వేయండి , దాని డిక్లరేషన్‌లో తిరిగి విసిరిన మినహాయింపులను సూచిస్తుంది .

"మళ్ళీ. మీ ప్రధాన పద్ధతికి ' ఫైల్‌నోట్‌ఫౌండ్‌ఎక్సెప్షన్ త్రోలు , …' డిక్లరేషన్‌ని కలిగి ఉన్న పద్ధతిని కాల్ చేయవలసి వస్తే , మీరు రెండు విషయాలలో ఒకటి చేయాలి:

1) FileNotFoundExceptionని పట్టుకోండి , …

మీరు తప్పనిసరిగా అసురక్షిత పద్ధతికి కాల్ చేసే కోడ్‌ను ట్రై-క్యాచ్ బ్లాక్‌లో చుట్టాలి.

2) FileNotFoundExceptionని పట్టుకోవద్దు , …

మీరు మీ ప్రధాన పద్ధతి యొక్క త్రోల జాబితాకు ఈ మినహాయింపులను తప్పనిసరిగా జోడించాలి ."

"మీరు నాకు ఒక ఉదాహరణ చెప్పగలరా?"

"ఇది చూడండి:"

తనిఖీ చేయబడిన మినహాయింపుల ఉదాహరణలు
public static void main(String[] args)
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"ఈ ఉదాహరణలోని కోడ్ కంపైల్ చేయబడదు, ఎందుకంటే ప్రధాన పద్ధతి పద్ధతి1() అని పిలుస్తుంది , ఇది క్యాచ్ చేయవలసిన మినహాయింపులను విసురుతుంది."

"దీన్ని కంపైల్ చేయడానికి, మేము ప్రధాన పద్ధతికి మినహాయింపు నిర్వహణను జోడించాలి. మీరు దీన్ని రెండు మార్గాలలో ఒకదానిలో చేయవచ్చు:"

ఎంపిక 1: మేము మినహాయింపును తిరిగి త్రోసిపుచ్చుతాము (కాలర్‌కు):
public static void main(String[] args)  throws FileNotFoundException, ClassNotFoundException
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"మరియు ఇక్కడ మేము దీనిని ట్రై-క్యాచ్‌తో పట్టుకున్నాము :"

ఎంపిక 2: మినహాయింపును పొందండి:
public static void main(String[] args)
{
    try
    {
        method1();
    }
    catch(Exception e)
    {
    }
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"ఇది మరింత స్పష్టంగా కనిపించడం ప్రారంభించింది."

"క్రింది ఉదాహరణ చూడండి. ఇది మీకు మిగిలిన వాటిని అర్థం చేసుకోవడానికి సహాయపడుతుంది."

మినహాయింపులను నిర్వహించడానికి బదులుగా, మేము వాటిని ఎలా నిర్వహించాలో తెలిసిన కాల్ స్టాక్‌లో ఉన్న పద్ధతులకు వాటిని తిరిగి పంపుతాము:
public static void method2() throws FileNotFoundException, ClassNotFoundException
{
    method1();
}
ఒక మినహాయింపును నిర్వహించండి మరియు మరొకటి వేయండి:
public static void method3() throws ClassNotFoundException
{
   try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
}
రెండు మినహాయింపులను పట్టుకోండి, ఏదీ వేయకండి:
public static void method4()
{
    try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
    catch (ClassNotFoundException e)
    {
        System.out.println("ClassNotFoundException has been caught.");
    }
}
3
టాస్క్
Java Syntax,  స్థాయిపాఠం
లాక్ చేయబడింది
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).

"మరో రకమైన మినహాయింపు ఉంది, రన్‌టైమ్ మినహాయింపు మరియు దానిని వారసత్వంగా పొందే తరగతులు. మీరు వాటిని పట్టుకోవాల్సిన అవసరం లేదు. ఇవి తనిఖీ చేయని మినహాయింపులు. వాటిని అంచనా వేయడం కష్టంగా పరిగణించబడుతుంది. మీరు వాటితో అదే విధంగా వ్యవహరించవచ్చు, కానీ మీరు వాటిని త్రోస్ క్లాజ్‌లో సూచించాల్సిన అవసరం లేదు ."