" మినహాయింపులు ఎలా పని చేస్తాయనే దాని గురించి నేను మీకు కొంచెం చెప్పాలనుకుంటున్నాను . దిగువ ఉదాహరణ మీకు ఏమి జరుగుతుందనే స్థూల ఆలోచనను ఇస్తుంది:"

మినహాయింపులను ఉపయోగించే కోడ్:
class ExceptionExampleOriginal
{


    public static void main(String[] args)
    {
        System.out.println("main begin");
        try
        {
            System.out.println("main before call");

            method1();



            System.out.println("main after call");
        }
        catch (RuntimeException e)
        {


            String s = e.getMessage();
            System.out.println(s);
        }
        System.out.println("main end");
    }

    public static void method1()
    {
        System.out.println("method1 begin");
        method2();

        System.out.println("method1 end");
    }

    public static void method2()
    {
      System.out.println("method2");
      String s = "Message: Unknown Exception";
      throw new RuntimeException(s);

    }
}
ఏమి జరుగుతుందో సుమారుగా ప్రాతినిధ్యం
public class ExceptionExample
{
    private static Exception exception = null;

   public static void main(String[] args)
    {
        System.out.println("main begin");

       
        System.out.println("main before call");

        method1();

        if (exception == null)
        {
            System.out.println("main after call");
        }
        else if (exception instanceof RuntimeException)
        {
            RuntimeException e = (RuntimeException) exception;
            exception = null;
            String s = e.getMessage();
            System.out.println(s);
        }
        System.out.println("main end");
    }

    public static void method1()
    {
        System.out.println("method1 begin");
        method2();
        if (exception != null) return;
        System.out.println("method1 end");
    }

    public static void method2()
    {
        System.out.println("method2");
        String s = "Message: Unknown Exception";
        exception = new RuntimeException(s);
        return;
    }
}

"నేను పూర్తిగా కోల్పోయాను."

"సరే. ఏం జరుగుతుందో వివరిస్తాను."

"ఎడమవైపున ఉన్న ఉదాహరణలో, మేము కొన్ని పద్ధతులను వరుసగా పిలుస్తాము. లో method2, ​​మేము ఉద్దేశపూర్వకంగా ఒక మినహాయింపును సృష్టిస్తాము మరియు విసిరాము (మేము ఒక లోపాన్ని సృష్టిస్తాము)."

"కుడివైపు ఉన్న ఉదాహరణ ఏమి జరుగుతుందో చూపిస్తుంది."

చూడండి method2_ RuntimeException_ exception_ return_

"లో method1, కాల్ చేసిన తర్వాత method2, మినహాయింపు ఉందో లేదో తనిఖీ చేస్తాము . మినహాయింపు ఉంటే, method1ఒకేసారి ముగుస్తుంది. జావాలో ప్రతి (!) మెథడ్ కాల్ తర్వాత ఇలాంటి చెక్ పరోక్షంగా నిర్వహించబడుతుంది."

"వావ్!"

"వావ్ కరెక్ట్."

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

"ఏమి చేయాలి throw మరియు instanceof అర్థం ఏమిటి? "

"చివరి పంక్తిని చూడండి: throw new RuntimeException(s);. ఇలా మీరు ఒక మినహాయింపును సృష్టించి, విసిరేస్తారు. మేము ఇంకా దానిపై పని చేయము. ఇది ఒక ఉదాహరణ మాత్రమే."

a instanceof B" ఆబ్జెక్ట్ aరకంగా ఉందో లేదో తనిఖీ చేయడానికి మేము ఉపయోగిస్తాము B, అనగా వేరియబుల్ మినహాయింపు ద్వారా సూచించబడిన వస్తువు RuntimeException కాదా. ఇది బూలియన్ వ్యక్తీకరణ."

"నాకు అర్థమైందని అనుకుంటున్నాను. దాదాపు."