2013年12月15日星期日

java exception in question

public class Excepts {
public static void  func1() throws Throwable
{
throw new Exception("from func1");
}
public static void main(String[] Str) throws Throwable
{
try
{
func1();
}
catch(Exception e)
//catch (Throwable e)
{
System.out.println("This is the main catch");
}
}

}

The question now is when I declare main function throws an exception after Throwable removed, there will be mistakes , but with throws Throwable no problem , I do not know why this is ? What is the relationship between the abnormal function of the body with the exception
thrown function declaration , what principles should meet ?
------ Solution ---------------------------------------- ----
for exception handling : If a method throws an exception , then call this method the caller must handle this exception , or throw on a caller. This looks like the time to write the code will be checked . main calls a function , you need to throw an exception handler slightly .
------ For reference only -------------------------------------- -
catch catch in the class is the parent class method declaration or itself.

func1 () statement throws Throwable, so the catch should be Throwable ( no parent a ) .
------ For reference only -------------------------------------- -
Exception inherited from Throwable, Exception is Throwable, but Throwable not Exception. So I can not catch that can only be thrown . Therefore, the main method should be thrown .
------ For reference only -------------------------------------- -

but I captured the above program exception , the results of the program are running :
This is the main catch
------ For reference only ------------------------- --------------

I do not mistake the above procedures , to capture the exception , the program runs the result is :
This is the main catch
------ For reference only ------------------------- --------------
halo, of course you print or print your sentence ah. After you catch the exception with e.printStack () you can see the effect of the

public class Excepts {
public static void func1() throws Throwable {
throw new Exception("from func1");
}

public static void main(String[] Str) throws Throwable {
try {
func1();

// catch (Exception e)
catch (Throwable e)
{
e.printStackTrace();
System.out.println("This is the main catch");
}
}

}


print :
java.lang.Exception: from func1
at com.withiter.test2.Excepts.func1 (Excepts.java: 5)
at com.withiter.test2.Excepts.main (Excepts.java: 10)
This is the main catch

------ For reference only ---------------------------------- -----
compiler is not so smart ? The compiler is probably just syntax checking , and running is not the same .
------ For reference only -------------------------------------- -
public class Excepts {
    public static void func1() throws Throwable {
        throw new Throwable ("from func1");
    }
 
    public static void main(String[] Str) throws Throwable {
        try {
            func1();
        } 
        catch (Exception e)
       // catch (Throwable e)
        {
            e.printStackTrace();
            System.out.println("This is the main catch");
        }
    }
 
}

This must be flat thrown
------ For reference only ---------------------------- -----------

try Throwable exception is thrown , but the catch (Exception e) to capture the exception, which is the reason why
----- - For reference only ---------------------------------------

As you said , it should be the main function of the exception is thrown to go, but I was in the main function to capture the exception , indicating that the main function does not throw an Throwable exception , since there is no throws Throwable exception , why I put main function throws Throwable statement to remove the error
------ For reference only happened --------------------------- ------------

I think you Mogao Qing I asked the question , my code runs without error , my question is, why did wrong , stands to reason that I release is Throwable exception, but Catch the exception exception , arguably the exception is a subclass of Throwable , it can not be captured , but here it captures the
------ For reference only ------- --------------------------------
Damn, what are answered Yeah .
feel the landlord is 2 , upstairs your mother is also very silent .



------ For reference only ---------------------------------- -----
Well, in fact, is to capture the range of small.
------ For reference only ---------------------------------------
1. compile-time checking and run is not the same , compile-time checking is limited checks.
2. compilers have a responsibility , not when you write . The method does not throw things inside , do you throw in the method head. If you are using the correct compiler in addition to the problem that the compiler can not be justified . Compile-time checking can be limited. wording
3.func1 () method is not typical . In general , the type should be up-conversion , like automatic numeric type implicit conversion . That should become a sub- class of the parent class , reversed back lost members , unpredictable problems occur.
------ For reference only -------------------------------------- -
type if it is down-converted , when you want to convert received over : (Exception) Throwable
another example :
Object o = "aaaaaaaaa"; .
((String) Object) subString (...);
------ For reference only -------------------- -------------------
type if it is down-converted , when you want to convert received over : (Exception) Throwable
another example :
Object o = "aaaaaaaaa";
((String) o) subString (...);.
------ For reference only -------------------- -------------------

try Throwable exception is thrown , but the catch (Exception e) to capture the exception, which is the reason why  

I just wanted to illustrate the relationship between the scope Throwable, Exception , where the catch (Exception e) has not been captured , System.out.println ("This is the main catch"); these are did not perform
------ For reference only ------------------------------------ ---

I do not mistake the above procedures , to capture the exception , the program runs the result is :   
This is the main catch  

method declares an exception A , then the caller must capture A ( except RuntimeException), which captures only exception is the child can not be compiled.

------ For reference only ---------------------------------- -----

not quality , at least in this loaded B, roll away point
------ For reference only -------------- -------------------------

try Throwable exception is thrown , but the catch (Exception e) captured exception, which is the reason why          
  
I just wanted to illustrate the relationship between the scope Throwable, Exception , where the catch (Exception e) has not been captured , System.out.println ("This is the main catch"); these are no execution  
you modify my program , you said the scope of these relationships , I know, but still thank you for your detailed explanation
------ For reference only ----------- ----------------------------

not quality , at least in this loaded B, roll away point  

landlord brain damage
your mother know you're so mentally it?
------ For reference only ---------------------------------------
when you throw the packaging into the Throwable! ! ! Then you use an Exception catch ? How to capture ? Only big catch small ! Not small catch big . Understand ?

you that method throws a new Exception, but when you throw , has become a throwable! So when you capture, capture only a throwable .
------ For reference only -------------------------------------- -

not quality , at least in this loaded B, roll away point          
  
landlord brain damage      
your mother know you're so mentally it?     
Your father does not know, but your uncle know

没有评论:

发表评论