黑马程序员——day10

 
 
/*
    finally代码块:定义一定执行的代码。
    通常用于关闭资源。
 
*/
class FuShuException extends Exception
{
    FuShuException(String msg)
    {
        super(msg);
    }
}
 
class Demo
{
    int div(int a,int b)throws FuShuException
    {
        if(b<0)
            throw new FuShuException("除数为负数");
        return a/b;
    }
}
 
class  ExceptionDemo5
{
    public static void main(String[] args)  
    {
        Demo d = new Demo();
 
        try
        {
            int x = d.div(4,-1);
            System.out.println("x="+x);
 
        }
        catch (FuShuException e)
        {
            System.out.println(e.toString());
            return;
            //System.exit(0);//系统,退出。jvm结束。
        }
        finally
        {
            System.out.println("finally");//finally中存放的是一定会被执行的代码。
        }
 
 
        System.out.println("over");
    }
}
 
 
class NoException extends Exception
{
}
 
public void method()throws NoException
{
 
    连接数据库;
 
    数据操作;//throw new SQLException();
 
    关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源。
 
 
    try
    {
        
        连接数据库;
 
        数据操作;//throw new SQLException();
    }
    catch (SQLException e)
    {
        会对数据库进行异常处理;
        throw new NoException();
 
    }
    finally
    {
        关闭数据库;
    }
 
 

}


 
第一个格式:
try
{
    
}
catch ()
{
}
 
第二个格式:
try
{
    
}
catch ()
{
}
finally
{
 
}
 
第三个格式:
try
{
    
}
finally
{
}
//记住一点:catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常。那么必须声明。
 
class Demo
{
    public void method()
    {
        try
        {
            throw new Exception();
        }
        finally
        {
            //关资源。
        }
    }
}
 
 
class   
{
    public static void main(String[] args)  
    {
        System.out.println("Hello World!");
    } 


}   

 
/*
异常在子父类覆盖中的体现;
1,子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
2,如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
3,如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。
    如果子类方法发生了异常。就必须要进行try处理。绝对不能抛。
 
*/
 
 
class AException extends Exception
{
}
 
class BException extends AException
{
}
 
class CException extends Exception
{
}
 
/*
Exception  
    |--AException
        |--BException
    |--CException
*/
class Fu
{
    void show()throws AException
    {
    
    }
}
 
class Test
{
    void function(Fu f)
    {
        try
        {
            f.show();
        }
        catch (AException e)
        {
 
        }
        
    }
}
 
 
class Zi extends Fu
{
    void show()throws CException
    {
        
    }
}
 
 
class   
{
    public static void main(String[] args)  
    {
        Test t = new Test();
        t.function(new Zi());
    }
}



/*
有一个圆形和长方形。
都可以获取面积。对于面积如果出现非法的数值,视为是获取面积出现问题。
问题通过异常来表示。
现有对这个程序进行基本设计。
 
*/
 
class NoValueException extends RuntimeException
{
    NoValueException(String message)
    {
        super(message);
    }
}
 
interface Shape
{
    void getArea();
}
 
class Rec implements Shape
{
    private int len,wid;
 
    Rec(int len ,int wid)//throws NoValueException
    {
        if(len<=0 || wid<=0)
            throw new NoValueException("出现非法值");
 
        this.len = len;
        this.wid = wid;
    }
 
    public void getArea()
    {
        System.out.println(len*wid);
    }
}
 
 
class Circle implements Shape
{
    private int radius;
    public static final double PI = 3.14;
 
    Circle(int radius)
    {
        if(radius<=0)
            throw new NoValueException("非法");
        this.radius = radius;
    }
 
    public void getArea()
    {
        System.out.println(radius*radius*PI);
    }
}
 
 
 
 
class  ExceptionTest1
{
    public static void main(String[] args)  
    {    
        
        Rec r = new Rec(3,4);
        r.getArea();
 
        Circle c = new Circle(-8);
 
        System.out.println("over");
    
    }
}


### 黑马程序员微服务 Day3 代码示例 #### Nacos 配置中心集成 Nacos作为配置中心和服务发现工具,在微服务架构中扮演着重要角色。为了实现配置动态刷新功能,可以创建`NacosConfig.java`来加载并监听配置变化。 ```java import com.alibaba.nacos.api.config.annotation.NacosValue; import org.springframework.stereotype.Component; @Component public class NacosConfig { @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true) private boolean useLocalCache; public boolean isUseLocalCache() { return useLocalCache; } } ``` 此段代码展示了如何通过`@NacosValue`注解注入来自Nacos服务器上的配置项,并设置自动刷新属性以便实时更新应用内的变量值[^4]。 #### Feign 客户端声明式HTTP请求 Feign是一个声明式的Web Service客户端,它使得编写HTTP API变得非常简单。下面的例子说明了怎样定义一个接口用于远程调用其他服务: ```java package cn.itcast.feign.clients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name="product-service") public interface ProductClient { @GetMapping("/products/{id}") String getProductById(@PathVariable("id") Long id); } ``` 上述代码片段定义了一个名为`ProductClient`的Feign Client, 使用`@FeignClient`指定目标服务名称以及具体的API路径和参数映射关系。 #### Gateway 网关路由规则配置 Spring Cloud Gateway提供了强大的路由能力,可以通过YAML文件轻松配置各种复杂的转发逻辑。以下是部分关于网关路由设定的内容摘录: ```yaml spring: cloud: gateway: routes: - id: product_route uri: lb://PRODUCT-SERVICE predicates: - Path=/api/products/** filters: - StripPrefix=1 ``` 这里设置了针对产品服务的一条路由规则,当接收到匹配前缀为`/api/products/`的请求时会将其转发给负载均衡器选择的具体实例处理,并去除URL中的第一个路径分段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值